PHP XML DOM Parser: Read, Modify & Create XML Like a Pro

Last updated 2 months, 3 weeks ago | 180 views 75     5

Tags:- PHP

Introduction: Why PHP XML DOM Parsing Matters

XML (eXtensible Markup Language) is a popular format for data exchange across APIs, config files, and web services. While PHP provides multiple ways to handle XML (SimpleXML, Expat, etc.), the DOM extension is the most powerful for creating, navigating, and modifying XML documents.

If you're working with complex XML files or need full control over nodes and attributes, the PHP XML DOM parser is your go-to solution.

In this article, you’ll learn how to:

  • Load and parse XML documents

  • Read and modify elements and attributes

  • Add or remove nodes dynamically

  • Create new XML files from scratch


What is PHP DOMDocument?

The DOMDocument class in PHP provides a tree-based structure for XML documents. You can traverse, edit, and manipulate any part of the XML tree—much like working with the Document Object Model in JavaScript.


Getting Started with PHP DOM

✅ Step 1: Load the XML

Load from a file:

$doc = new DOMDocument();
$doc->load('books.xml');

Load from a string:

$doc = new DOMDocument();
$doc->loadXML($xmlString);

Use loadXML() for raw XML content and load() for XML files.


✅ Step 2: Read XML Elements

Use getElementsByTagName() to access specific tags.

$titles = $doc->getElementsByTagName('title');

foreach ($titles as $title) {
    echo $title->nodeValue . "<br>";
}

✅ Step 3: Access Element Attributes

$books = $doc->getElementsByTagName('book');

foreach ($books as $book) {
    echo $book->getAttribute('id') . "<br>";
}

✅ Step 4: Modify Existing Nodes

$books = $doc->getElementsByTagName('book');
$firstBook = $books->item(0);

// Change the title
$firstBook->getElementsByTagName('title')->item(0)->nodeValue = 'New Title';

✅ Step 5: Add New Elements

$newBook = $doc->createElement('book');
$newBook->setAttribute('id', '103');

$title = $doc->createElement('title', 'PHP for Experts');
$author = $doc->createElement('author', 'Mark Ray');

$newBook->appendChild($title);
$newBook->appendChild($author);

// Append to root node
$doc->documentElement->appendChild($newBook);

✅ Step 6: Save Changes to File

$doc->save('updated_books.xml');

Complete Example: Reading & Editing XML with DOM

books.xml

<catalog>
    <book id="001">
        <title>Learn PHP</title>
        <author>Jane Smith</author>
    </book>
    <book id="002">
        <title>Advanced PHP</title>
        <author>John Doe</author>
    </book>
</catalog>

dom_parser.php

<?php
// Load XML
$doc = new DOMDocument();
$doc->load('books.xml');

// Display all titles
$titles = $doc->getElementsByTagName('title');
foreach ($titles as $title) {
    echo "Title: " . $title->nodeValue . "<br>";
}

// Modify first book's title
$books = $doc->getElementsByTagName('book');
$books->item(0)->getElementsByTagName('title')->item(0)->nodeValue = 'PHP Crash Course';

// Add a new book
$newBook = $doc->createElement('book');
$newBook->setAttribute('id', '003');

$title = $doc->createElement('title', 'Mastering PHP');
$author = $doc->createElement('author', 'Alice Johnson');

$newBook->appendChild($title);
$newBook->appendChild($author);
$doc->documentElement->appendChild($newBook);

// Save modified XML
$doc->save('books_updated.xml');
?>

⚠️ Tips & Common Pitfalls

Best Practices

  • Always call $doc->preserveWhiteSpace = false; before loading XML to avoid extra whitespace.

  • Use $doc->formatOutput = true; to save pretty-printed XML.

  • Use DOMXPath for advanced XML queries (like XPath filters).

Common Mistakes

  • Forgetting to save the file after modifications using $doc->save().

  • Incorrect node referencing: Always ensure you use item(0) to access the actual DOMNode from a NodeList.

  • Assuming elements exist: Use hasChildNodes() or getElementsByTagName()->length to verify before accessing nodes.


Comparison: DOM vs SimpleXML vs Expat

Feature DOMDocument SimpleXML Expat (xml)
XML Tree Navigation ✅ Full control ✅ Easy ❌ Not supported
Attribute Manipulation ✅ Detailed ⚠️ Limited ✅ via callbacks
Modify/Create XML ✅ Powerful ⚠️ Basic ❌ Read-only
Memory Usage ⚠️ Higher ✅ Medium ✅ Efficient
Best Use Case Complex XML Simple reads Streaming data

✅ Conclusion: When and Why to Use PHP DOM

The PHP DOM extension is the most robust XML tool when you need to:

  • Read, edit, and create complex XML files

  • Perform fine-grained manipulations

  • Work with attributes, nested structures, or namespaces

Best Practices Recap

  • Use DOMDocument when you need structure + flexibility.

  • Handle nodes and attributes with care—DOM is strict but powerful.

  • Combine with DOMXPath for querying complex documents.