Working with XML in PHP: A Complete Guide for Developers
XML (eXtensible Markup Language) remains one of the key formats for data exchange, application configuration, and integration with external services. PHP provides several powerful tools for working with XML, each suited for different scenarios. In this article, we will thoroughly examine the main libraries and approaches used in modern PHP development.
Main PHP Tools for Working with XML
PHP has three main extensions for working with XML, each with its own features and areas of application:
- SimpleXML — a simple and intuitive parser for reading XML. Ideal for small files and quick processing.
- DOMDocument — a full implementation of the W3C DOM for working with XML documents. Supports reading, writing, modification, and validation.
- XMLReader / XMLWriter — streaming parsers for working with large XML files (hundreds of megabytes and gigabytes).
1. SimpleXML — Quick Start
SimpleXML allows you to convert an XML string or file into an object that can be worked with like a regular array or object. This is the most popular choice for simple tasks.
<?php// Reading XML from a string$xmlString = '<books> <book id="1"> <title>PHP for Beginners</title> <author>Ivan Petrov</author> <price currency="RUB">1500</price> </book> <book id="2"> <title>XML in Detail</title> <author>Anna Smirnova</author> <price currency="RUB">2000</price> </book></books>';
$xml = simplexml_load_string($xmlString);
// Accessing dataforeach ($xml->book as $book) { echo "ID: " . $book['id'] . ""; echo "Title: " . $book->title . ""; echo "Author: " . $book->author . ""; echo "Price: " . $book->price . " " . $book->price['currency'] . "
";}
// Creating new XML$newXml = new SimpleXMLElement('<catalog/>');$product = $newXml->addChild('product');$product->addAttribute('sku', 'P001');$product->addChild('name', 'Laptop');$product->addChild('price', 55000);
echo $newXml->asXML();?>2. DOMDocument — Full Control
DOMDocument provides full access to the Document Object Model. You can create, modify, and delete any nodes, as well as execute XPath queries to search for data.
<?php$dom = new DOMDocument('1.0', 'UTF-8');$dom->formatOutput = true;
// Creating the root element$root = $dom->createElement('employees');$dom->appendChild($root);
// Adding an employee$employee = $dom->createElement('employee');$employee->setAttribute('id', '101');
$name = $dom->createElement('name', 'Alexey Kuznetsov');$employee->appendChild($name);
$position = $dom->createElement('position', 'Developer');$employee->appendChild($position);
$root->appendChild($employee);
// XPath query$xpath = new DOMXPath($dom);$query = "//employee[@id='101']/name";$result = $xpath->query($query);
if ($result->length > 0) { echo "Found: " . $result->item(0)->nodeValue;}
// Saving to file$dom->save('employees.xml');?>3. XMLReader and XMLWriter — For Large Data
When XML files become too large to load into memory (over 10-50 MB), streaming parsers come to the rescue. XMLReader reads the document node by node, while XMLWriter creates XML in parts.
<?php// Reading a large XML file$reader = new XMLReader();$reader->open('large_data.xml');
while ($reader->read( Also in library
Knex.js for JavaScript (Node.js): Complete Guide with Examples
Internationalization (i18n) in PHP: gettext library and modern approaches