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.1 XML manuell generieren

Problem

Sie möchten XML generieren. Beispielsweise möchten Sie eine XML-Version Ihrer Daten erzeugen, die von einem anderen Programm gelesen werden kann.

Lösung

Lassen Sie Ihre Daten in einer Schleife durchlaufen und geben Sie sie in passende XML-Tags eingeschlossen aus:

header('Content-Type: text/xml'); print '<?xml version="1.0"?>' . "\n"; print "<shows>\n"; $shows = array(array('name' => 'Simpsons', 'kanal' => 'FOX', 'beginn' => '20:00', 'dauer' => '30'), array('name' => 'Law & Order', 'kanal' => 'NBC', 'beginn' => '20:00', 'dauer' => '60')); foreach ($shows as $show) { print " <show>\n"; foreach($show as $tag => $data) { print " <$tag>" . htmlspecialchars($data) . "</$tag>\n"; } print " </show>\n"; } print "</shows>\n"; ...

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