O'Reilly logo

PHP 5 Kochbuch, Third Edition by Stephan Schmidt, Ulrich Speidel, Matthias Brusdeylins, Carsten Lucke, Adam Trachtenberg, David Sklar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

14.14 XML-Dokumente in PHP-Arrays oder Objekte einlesen

Problem

Sie möchten ein XML-Dokument in PHP-Arrays oder Objekte einlesen, ohne sich mit einer der XML-Erweiterungen befassen zu müssen.

Lösung

Verwenden Sie die Klasse XML_Unserializer des PEAR-Pakets XML_Serializer.

require_once 'XML/Unserializer.php';

$xml = <<<EOD
<shows>
    <show>
        <name>Simpsons</name>
        <kanal>FOX</kanal>
        <beginn>20:00</beginn>
        <dauer>30</dauer>
    </show>
    <show>
        <name>Law &amp; Order</name>
        <kanal>NBC</kanal>
        <beginn>20:00</beginn>
        <dauer>60</dauer>
    </show>
</shows>
EOD;

$unserializer = new XML_Unserializer();
$success = $unserializer->unserialize($xml);
if (PEAR::isError($success)) {
    die($success->getMessage());
}
$data = $unserializer->getUnserializedData();

print_r($data);

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required