Model structured documents with elements, attributes, namespaces, schemas, parsing, and transformations.
Lessons
Read XML incrementally with a pull parser.
Pull Parsing (StAX) is easiest to learn by reading the example, changing it, and observing the result.
<?php
// Stream large XML without loading it all into memory
$reader = new XMLReader();
$reader->open('huge-feed.xml');
while ($reader->read()) {
if ($reader->nodeType === XMLReader::ELEMENT && $reader->name === 'item') {
$node = simplexml_load_string($reader->readOuterXML());
echo $node->title, "
";
}
}
$reader->close();Practice the Pull Parsing (StAX) example in a small scratch file, then explain what changed and why.