PHP XML DOM


PHP provides the DOM (Document Object Model) parser to handle XML files in a structured, object-oriented way. Unlike SimpleXML, which is easy for reading, DOM allows you to create, edit, delete, and traverse XML nodes.

The DOM parser represents an XML document as a tree structure, where each element, attribute, or piece of text is a node. This gives you complete control over the XML data, making it ideal for tasks that require modifications or detailed processing.

What Is DOM Parser?

The DOM parser loads an XML document entirely into memory and represents it as a tree. You can then:

  • Access nodes directly

  • Navigate parent, child, and sibling nodes

  • Add, modify, or remove elements and attributes

  • Save the XML back to a file

Because of this, DOM is more memory-intensive than SimpleXML, but it’s much more powerful for editing or creating XML files.

Example XML File

We will use a students.xml file:

<students>
  <student id="1">
    <name>Riya Sharma</name>
    <age>20</age>
    <course>Web Development</course>
  </student>
  <student id="2">
    <name>Ananya Singh</name>
    <age>22</age>
    <course>Data Science</course>
  </student>
  <student id="3">
    <name>Meera Patel</name>
    <age>21</age>
    <course>UI/UX Design</course>
  </student>
</students>

Loading an XML File with DOM

Use the DOMDocument class to load XML:

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

The $dom object now represents the entire XML document.

Accessing Elements

You can get elements by tag name using getElementsByTagName():

<?php
$students = $dom->getElementsByTagName("student");

foreach ($students as $student) {
    echo "Name: " . $student->getElementsByTagName("name")[0]->nodeValue . "<br>";
    echo "Age: " . $student->getElementsByTagName("age")[0]->nodeValue . "<br>";
    echo "Course: " . $student->getElementsByTagName("course")[0]->nodeValue . "<br><br>";
}
?>

Explanation

  • getElementsByTagName("student") returns a list of all <student> elements.

  • Each element is a DOMNode, and you can get child node values with nodeValue.

  • Use [0] to access the first matching child element for each parent.

Accessing Attributes

To read an attribute of an element, use the getAttribute() method:

<?php
foreach ($students as $student) {
    echo "Student ID: " . $student->getAttribute("id") . "<br>";
}
?>

This outputs all student IDs from the XML file.

Adding a New Element

DOM allows you to create new nodes and append them to the XML tree:

<?php
$newStudent = $dom->createElement("student");
$newStudent->setAttribute("id", "4");

$name = $dom->createElement("name", "Isha Kapoor");
$age = $dom->createElement("age", "23");
$course = $dom->createElement("course", "Cloud Computing");

$newStudent->appendChild($name);
$newStudent->appendChild($age);
$newStudent->appendChild($course);

$dom->documentElement->appendChild($newStudent);
$dom->save("students.xml");

echo "New student added successfully!";
?>

Explanation

  • createElement() creates a new element.

  • setAttribute() adds attributes.

  • appendChild() adds a child element to its parent.

  • documentElement represents the root element (<students> in this case).

Modifying Existing Elements

You can also modify the value of a node:

<?php
$students = $dom->getElementsByTagName("student");
$students->item(0)->getElementsByTagName("course")[0]->nodeValue = "Full Stack Development";
$dom->save("students.xml");

echo "Student course updated successfully!";
?>

Explanation

  • item(0) selects the first <student> node.

  • nodeValue can be updated directly.

  • Always call save() to persist changes to the file.

Deleting an Element

DOM allows you to remove elements from the XML tree:

<?php
$students = $dom->getElementsByTagName("student");
$dom->documentElement->removeChild($students->item(1));
$dom->save("students.xml");

echo "Second student removed successfully!";
?>

This deletes the second <student> element from the XML file.

Navigating the DOM Tree

DOM allows you to move around the tree easily:

  • parentNode – Access the parent of a node

  • childNodes – Access all child nodes

  • nextSibling / previousSibling – Access neighboring nodes

Example: Print names of students using child nodes:

<?php
$students = $dom->getElementsByTagName("student");

foreach ($students as $student) {
    foreach ($student->childNodes as $child) {
        if ($child->nodeName == "name") {
            echo $child->nodeValue . "<br>";
        }
    }
}
?>

Error Handling

You can enable error checking while loading XML:

<?php
libxml_use_internal_errors(true);
$dom = new DOMDocument();
if (!$dom->load("students.xml")) {
    foreach (libxml_get_errors() as $error) {
        echo $error->message . "<br>";
    }
} else {
    echo "XML loaded successfully!";
}
?>

This prevents PHP from throwing warnings for malformed XML files.

Comparing DOM with SimpleXML

Feature SimpleXML DOM
Ease of use Easy Moderate
Load entire file Yes Yes
Memory usage Low–Moderate High
Edit XML Limited Full control
Add/Delete nodes Limited Yes
Best for Reading small files Editing, creating, or complex manipulation

Summary of the Tutorial

The DOM parser in PHP is a powerful tool for working with XML files. You can read, modify, add, and delete nodes, navigate the XML tree, and handle attributes and nested elements.

Key takeaways:

  • Use DOMDocument to load and manipulate XML.

  • Access elements with getElementsByTagName() and nodeValue.

  • Modify or create new elements with createElement() and appendChild().

  • Remove elements with removeChild().

  • Use error handling to safely load XML files.

DOM is ideal when you need full control over XML or when you want to edit or generate XML documents programmatically.


Practice Questions

  1. Load students.xml using DOMDocument and print the name of each student.

  2. Access and display the id attribute of each <student> element.

  3. Add a new <student> element with name, age, and course, and save it to the XML file.

  4. Modify the <course> value of the first student to “Full Stack Development” and save the changes.

  5. Delete the second <student> element from students.xml using DOM methods.

  6. Loop through all <student> elements and print each child node name and value dynamically.

  7. Access a nested element (like <details> or <address> if present) and print its content.

  8. Count the total number of <student> elements and display the count.

  9. Implement error handling to safely load a malformed or missing XML file and print errors if any.

  10. Use DOM methods to loop through all attributes of each <student> element and display the attribute names and values.


Try a Short Quiz.

No quizzes available.


Online Code Editor and Compilor

Write and run code directly in your browser. Live preview for frontend languages.


Go Back Top