Working with XML in PHP: A Complete Guide to Parsing and Creating XML Documents

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with XML in PHP: From Simple Parsing to Creating Complex Documents



XML (eXtensible Markup Language) remains one of the key formats for data exchange, configuration files, and web services (SOAP, RSS/Atom). PHP provides several powerful built-in extensions for working with XML that allow you to solve almost any task — from parsing simple RSS feeds to generating complex documents with namespaces. In this article, we will cover the main tools: SimpleXML, DOMDocument, and XMLReader, and also learn how to create XML documents on the fly.



1. SimpleXML — Fast and Convenient Parsing



SimpleXML is the simplest extension for working with XML. It converts an XML document into an object whose properties can be read like regular arrays and objects. It is ideal for small and medium documents when complex navigation is not required.



Loading XML from a String or File



<?php// From a string$xmlString = '<book><title>PHP for Beginners</title><author>Ivan Petrov</author></book>';$book = simplexml_load_string($xmlString);

// From a file$books = simplexml_load_file('books.xml');

echo $book->title; // PHP for Beginnersecho $book->author; // Ivan Petrov?>


Working with Attributes and Multiple Elements



<?php$xml = '<library>    <book id="1">        <title>PHP Objects</title>        <author>Matt Zandstra</author>    </book>    <book id="2">        <title>Clean Code</title>        <author>Robert C. Martin</author>    </book></library>';

$library = simplexml_load_string($xml);

foreach ($library->book as $book) { echo 'ID: ' . $book['id'] . ' — ' . $book->title . ' (' . $book->author . ')<br>';}// ID: 1 — PHP Objects (Matt Zandstra)// ID: 2 — Clean Code (Robert C. Martin)?>


Important: SimpleXML does not support complex XPath queries with conditions (only simple paths). For more powerful queries, use DOMDocument or XPath.



2. DOMDocument — Full Control Over XML



DOMDocument implements the W3C DOM API and gives you full control over the document structure. You can add, delete, modify any nodes, work with namespaces, and execute XPath queries of any complexity. It is the choice for professional tasks.



Parsing and Navigation with DOMDocument



<?php$dom = new DOMDocument();$dom->load('books.xml');

// Get all <book> elements$books = $dom->getElementsByTagName('book');

foreach ($books as $book) { $title = $book->getElementsByTagName('title')->item(0)->nodeValue; $author = $book->getElementsByTagName('author')->item(0)->nodeValue; echo "Book: $title, Author: $author<br>";}?>


XPath — Powerful Queries to XML



<?php$dom = new DOMDocument();$dom->load('library.xml');$xpath = new DOMXPath($dom);

// Find all books with price > 500$expensiveBooks = $xpath->query('//book[price > 500]');

foreach ($expensiveBooks as $book) { echo $book->getElementsByTagName('title')->item(0)->nodeValue . ' — ' . $book->getElementsByTagName('price')->item(0)->nodeValue . ' rub.<br>';}?>


3. Creating XML Documents



PHP allows you not only to read but also to create XML documents. Let's look at both approaches.



Creating XML via SimpleXML



<?php$xml = new SimpleXMLElement('<catalog></catalog>');

$product = $xml->addChild('product');$product->addChild('name', 'Laptop');$product->addChild('price', 75000);$product->addAttribute('id', '12

Recommendations