14.11 Informationen aus einem XML-Dokument selektieren

Problem

Sie möchten auf einfache Art auf bestimmte Informationen in einem XML-Dokument abhängig von Tag-Namen und Attributwerten zugreifen.

Lösung

Verwenden Sie die XPath-Funktionen der DOM-Erweiterung, um einzelne Knoten zu extrahieren und weiterzuverarbeiten.

$xml = <<<EOD <team name="JLA"> <held alias="Superman"> <name>Clark Kent</name> <stadt>Metropolis</stadt> </held> <held alias="Batman"> <name>Bruce Wayne</name> <stadt>Gotham City</stadt> </held> <held alias="Der Rote Blitz"> <name>Wally West</name> <stadt>Keystone City</stadt> </held> </team> EOD; // Neues Dokument erzeugen. $dom = new DOMDocument(); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; // XML parsen. $dom->loadXML($xml); ...

Get PHP 5 Kochbuch now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.