O'Reilly logo

Programming PHP, 3rd Edition by Peter MacIntyre, Kevin Tatroe, Rasmus Lerdorf

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

Parsing XML with SimpleXML

If you’re consuming very simple XML documents, you might consider the third library provided by PHP, SimpleXML. SimpleXML doesn’t have the ability to generate documents as the DOM extension does, and isn’t as flexible or memory-efficient as the event-driven extension, but it makes it very easy to read, parse, and traverse simple XML documents.

SimpleXML takes a file, string, or DOM document (produced using the DOM extension) and generates an object. Properties on that object are counters providing access to elements in each node. Using them, you can access elements using numeric indices and nonnumeric indices to access attributes. Finally, you can use string conversion on any value you retrieve to get the text value of the item.

For example, we could display all the titles of the books in our books.xml document using:

$document = simplexml_load_file("books.xml");

foreach ($document->book as $book) {
  echo $book->title . "\r\n";
}

Using the children() method on the object, you can iterate over the child nodes of a given node; likewise, you can use the attributes() method on the object to iterate over the attributes of the node:

$document = simplexml_load_file("books.xml");

foreach ($document->book as $node) {
   foreach ($node->attributes() as $attribute) {
      echo "{$attribute}\n";
   }
}

Finally, using the asXml() method on the object, you can retrieve the XML of the document in XML format. This lets you change values in your document and write it back out to disk

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