Model structured documents with elements, attributes, namespaces, schemas, parsing, and transformations.
Lessons
Read XML with SimpleXML and DOMDocument.
Parsing XML in PHP is easiest to learn by reading the example, changing it, and observing the result.
<?php
// SimpleXML: quick, readable access
$xml = simplexml_load_file('lessons.xml');
foreach ($xml->lesson as $lesson) {
echo $lesson->title . ' [' . $lesson['level'] . "]
";
}
// DOMDocument: full control for editing
$dom = new DOMDocument();
$dom->load('lessons.xml');
$titles = $dom->getElementsByTagName('title');
echo $titles->item(0)->nodeValue, "
";Practice the Parsing XML in PHP example in a small scratch file, then explain what changed and why.