Working with XML in PHP: DOMDocument, SimpleXML and XPath
XML (eXtensible Markup Language) remains one of the key formats for data exchange, storing configurations, and integrating with external services (RSS feeds, SOAP, API). PHP provides several powerful built-in extensions for working with XML. In this article, we will cover three main approaches: DOMDocument (Document Object Model), SimpleXML (simple and intuitive), and XPath (query language for navigation).
1. Preparation: example XML document
For all examples, we will use the same XML file catalog.xml:
<?xml version="1.0" encoding="UTF-8"?><catalog> <book id="1"> <title>PHP for Beginners</title> <author>Ivan Petrov</author> <price currency="RUB">1200</price> </book> <book id="2"> <title>XML in Detail</title> <author>Maria Sidorova</author> <price currency="RUB">950</price> </book></catalog>2. DOMDocument — full control over XML
The DOMDocument extension implements W3C DOM Level 2. It provides maximum control: you can create, delete, modify any nodes, attributes, and text content. Suitable for complex scenarios where precise structure management is required.
Loading and reading XML
<?php$dom = new DOMDocument();$dom->load('catalog.xml'); // or loadXML($xmlString)
$books = $dom->getElementsByTagName('book');foreach ($books as $book) { $id = $book->getAttribute('id'); $title = $book->getElementsByTagName('title')->item(0)->nodeValue; $author = $book->getElementsByTagName('author')->item(0)->nodeValue; $price = $book->getElementsByTagName('price')->item(0)->nodeValue; echo "Book #$id: $title — $author, $price RUB.";}?>Creating new XML
<?php$dom = new DOMDocument('1.0', 'UTF-8');$root = $dom->createElement('catalog');$dom->appendChild($root);
$book = $dom->createElement('book');$book->setAttribute('id', '3');$title = $dom->createElement('title', 'PHP and XML');$book->appendChild($title);$root->appendChild($book);
echo $dom->saveXML(); // outputs XML string// $dom->save('new_catalog.xml');?>3. SimpleXML — simplicity and convenience
SimpleXML is a lighter extension that converts XML into an object with element access via properties. Ideal for quick reading, especially when the XML structure is known and does not require fine-tuning.
Reading XML with SimpleXML
<?php$xml = simplexml_load_file('catalog.xml'); // or simplexml_load_string($xmlString)
foreach ($xml->book as $book) { $id = (string)$book['id']; $title = (string)$book->title; $author = (string)$book->author; $price = (string)$book->price; echo "Book #$id: $title — $author, $price RUB.";}?>Creating XML with SimpleXML
<?php$xml = new SimpleXMLElement('<catalog></catalog>');$book = $xml->addChild('book');$book->addAttribute('id', '4');$book->addChild('title', 'SimpleXML Guide');$book->addChild('price', 800)->addAttribute('currency', 'RUB');
echo $xml->asXML();// $xml->asXML('output.xml');?>Modifying data
<?php$xml = simplexml_load_file('catalog.xml');$xml->book[0]->price = 1300; // change the price of the first book$xml->asXML('catalog_updated.xml');?>