Generate XML as a String

Loop through the data and print it out surrounded by the correct XML tags:

<?php
header('Content-Type: text/xml');
print '<?xml version="1.0"?>' . "\n";
print "<shows>\n";
 
$shows = array(array('name'     => 'Show1',
                     'channel'  => 'FOXchannel',
                     'start'    => '8:00 PM',
                     'duration' => '30'),
 
               array('name'     => 'Show2',
                     'channel'  => 'NBC',
                     'start'    => '8:00 PM',
                     'duration' => '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";
?>

Output:

<?xml version="1.0"?>
<shows>
    <show>
        <name>show1</name>
        <channel>FOXchannel</channel>
        <start>8:00 PM</start>
        <duration>30</duration>
    </show>
    <show>
        <name>show2</name>
        <channel>NBC</channel>
        <start>8:00 PM</start>
        <duration>60</duration>
    </show>
</shows>
Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

 

 
eXTReMe Tracker