CPSC415A
Web Programming

Day 16 Notes

PHP Simple XML

XML

<?xml version="1.0" encoding="UTF-8"?>
<useless>
  <entry verbosity="high">
    <one>this is the one tag data</one>
    <two>this is the two tag data</two>
  </entry>
  <entry verbosity="low">
    <one>1</one>
    <two>2</two>
  </entry>
</useless>

Parsing XML

$xml = simplexml_load_file("useless.xml");
foreach ($xml->entry as $anEntry) {
    print("Verbosity Attribute: " . $anEntry["verbosity"] . "<br />");
    print("One Tag: " . $anEntry->one . "<br />");
    print("Two Tag: " . $anEntry->two . "<br /><br />");
}

Modifying XML

$xml->entry[0]->one = "this is the updated one tag data";
$xml->entry[0]->addChild("three", "this is the three tag data");
unset($xml->entry[0]->two);
$xml->asXML("useless.xml");