-
Hajipur, Bihar, 844101
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.
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.
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>
Use the DOMDocument class to load XML:
<?php
$dom = new DOMDocument();
$dom->load("students.xml");
?>
The $dom object now represents the entire XML document.
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>";
}
?>
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.
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.
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!";
?>
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).
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!";
?>
item(0) selects the first <student> node.
nodeValue can be updated directly.
Always call save() to persist changes to the file.
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.
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>";
}
}
}
?>
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.
| 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 |
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.
Load students.xml using DOMDocument and print the name of each student.
Access and display the id attribute of each <student> element.
Add a new <student> element with name, age, and course, and save it to the XML file.
Modify the <course> value of the first student to “Full Stack Development” and save the changes.
Delete the second <student> element from students.xml using DOM methods.
Loop through all <student> elements and print each child node name and value dynamically.
Access a nested element (like <details> or <address> if present) and print its content.
Count the total number of <student> elements and display the count.
Implement error handling to safely load a malformed or missing XML file and print errors if any.
Use DOM methods to loop through all attributes of each <student> element and display the attribute names and values.