PHP XML DOM
Manipulate XML documents using the Document Object Model
🌳 What is XML DOM?
XML DOM (Document Object Model) treats XML documents as a tree structure. You can navigate, search, modify, and create XML elements easily using PHP's DOMDocument class.
<?php
// Create new DOM document
$dom = new DOMDocument();
$dom->loadXML("<root><item>Hello DOM!</item></root>");
echo $dom->saveXML();
?>
Output:
<?xml version="1.0"?>
<root><item>Hello DOM!</item></root>
Key DOM Concepts
The DOM represents XML as a hierarchical tree where each element, attribute, and text is a node. You can traverse this tree, modify nodes, add new ones, or remove existing elements programmatically.
Load XML
Load XML from string or file
$dom = new DOMDocument();
$dom->load("file.xml");
Query Elements
Find elements by tag name
$items = $dom
->getElementsByTagName("item");
Create Elements
Add new XML elements
$element = $dom
->createElement("item", "text");
Save XML
Output XML to string or file
$dom->save("output.xml");
echo $dom->saveXML();
🔹 Loading XML Documents
Load XML from different sources:
<?php
// Method 1: Load from string
$dom = new DOMDocument();
$xmlString = "<books><book>PHP Guide</book></books>";
$dom->loadXML($xmlString);
echo "Loaded from string<br>";
// Method 2: Load from file
$dom2 = new DOMDocument();
$dom2->load("books.xml");
echo "Loaded from file<br>";
// Method 3: Create new document
$dom3 = new DOMDocument("1.0", "UTF-8");
$root = $dom3->createElement("catalog");
$dom3->appendChild($root);
echo "Created new document";
?>
Output:
Loaded from file
Created new document
🔹 Reading XML Elements
Access and display XML element values:
<?php
$xml = "<library>
<book>PHP Basics</book>
<book>Advanced PHP</book>
<book>PHP Security</book>
</library>";
$dom = new DOMDocument();
$dom->loadXML($xml);
// Get all book elements
$books = $dom->getElementsByTagName("book");
echo "Total books: " . $books->length . "<br><br>";
// Loop through books
foreach ($books as $book) {
echo "📚 " . $book->nodeValue . "<br>";
}
?>
Output:
📚 PHP Basics
📚 Advanced PHP
📚 PHP Security
🔹 Working with Attributes
Read and set XML element attributes:
<?php
$xml = '<books>
<book id="101" lang="en">PHP Guide</book>
<book id="102" lang="es">Guía PHP</book>
</books>';
$dom = new DOMDocument();
$dom->loadXML($xml);
$books = $dom->getElementsByTagName("book");
foreach ($books as $book) {
$id = $book->getAttribute("id");
$lang = $book->getAttribute("lang");
$title = $book->nodeValue;
echo "Book #$id ($lang): $title<br>";
}
?>
Output:
Book #102 (es): Guía PHP
🔹 Creating XML Documents
Build XML documents from scratch:
<?php
// Create document
$dom = new DOMDocument("1.0", "UTF-8");
$dom->formatOutput = true;
// Create root element
$root = $dom->createElement("students");
$dom->appendChild($root);
// Create student element
$student = $dom->createElement("student");
$root->appendChild($student);
// Add name element
$name = $dom->createElement("name", "John Doe");
$student->appendChild($name);
// Add age element
$age = $dom->createElement("age", "20");
$student->appendChild($age);
// Add attribute
$student->setAttribute("id", "S001");
// Output XML
echo htmlspecialchars($dom->saveXML());
?>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="S001">
<name>John Doe</name>
<age>20</age>
</student>
</students>
🔹 Modifying XML
Update existing XML elements and attributes:
<?php
$xml = '<product id="100">
<name>Laptop</name>
<price>500</price>
</product>';
$dom = new DOMDocument();
$dom->loadXML($xml);
// Change price
$price = $dom->getElementsByTagName("price")->item(0);
$price->nodeValue = "450";
// Update attribute
$product = $dom->getElementsByTagName("product")->item(0);
$product->setAttribute("id", "101");
// Add new element
$stock = $dom->createElement("stock", "In Stock");
$product->appendChild($stock);
echo htmlspecialchars($dom->saveXML());
?>
Output:
<product id="101"> <name>Laptop</name> <price>450</price> <stock>In Stock</stock> </product>