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.13 XML-Dokumente aus PHP-Datenstrukturen erzeugen

Problem

Sie möchten aus einem Array oder PHP-Objekt ein XML-Dokument erzeugen.

Lösung

Verwenden Sie das PEAR-Paket XML_Serializer.

require_once 'XML/Serializer.php';

$shows = array(array('name'     => 'Simpsons',
                     'kanal'    => 'FOX',
                     'beginn'   => '20:00',
                     'dauer'    => '30'),

               array('name'     => 'Law & Order',
                     'kanal'    => 'NBC',
                     'beginn'   => '20:00',
                     'dauer'    => '60'));

$serializer = new XML_Serializer();
$serializer->setOption('indent', '    ');
$serializer->setOption('rootName', 'shows');
$serializer->setOption('defaultTagName', 'show');

$success = $serializer->serialize($shows);
if (PEAR::isError($success)) {
    die($success->getMessage());
}
print $serializer->getSerializedData();

Diskussion

Obwohl PHP 5 verschiedene ...

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