PHP DOM Parser
The PHP DOM Parser
The DOM parser makes it possible to parse XML documents in PHP.
The DOM parser is a tree-based parser and loads the entire document in memory as a tree structure. It analyzes the whole document, and provides access to the tree elements (DOM).
A tree-based parser is ideal for smaller XML documents, but not for large XML documents, as it can be very memory intensive!
Look at the following XML document fraction:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
The DOM parser sees the XML above as a tree structure:
- Root element: <note>
- Child element: <to>
- Text element: "Tove"
Tip: The DOM Parser is a part of the PHP core. No installation is required!
DOM Parser - Load and Output XML
The XML file "note.xml" will be used in the examples below.
The PHP DOM parser uses the DOMDocument class to parse documents into a Document Object Model (DOM) structure.
In the following example we initialize the DOM parser, and loads the XML from "note.xml" into it. Then the saveXML() function saves the XML document into a string, so we can output it:
<?php
// Initialize DOM parser
$xmlDoc = new DOMDocument();
// Load XML file
$xmlDoc->load("note.xml");
// Saving all the document
print $xmlDoc->saveXML();
?>
The output of the code above will be:
Tove Jani Reminder Don't forget me this weekend!
If you select "View source" in the browser window, you will see the following HTML:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
DOM Parser - Loop through XML
In the following example we initialize the DOM parser, and loads the XML from "note.xml" into it. Then, we loop through all child nodes of the <note> element:
<?php
// Initialize DOM parser
$xmlDoc = new DOMDocument();
// Load XML file
$xmlDoc->load("note.xml");
$x = $xmlDoc->documentElement;
// Loop through child nodes
foreach ($x->childNodes AS $item) {
print $item->nodeName . " = " . $item->nodeValue . "<br>";
}
?>
The output of the code above will be:
#text =
to = Tove
#text =
from = Jani
#text =
heading = Reminder
#text =
body = Don't forget me this weekend!
#text =
Tip: In the example above there are empty text nodes between each element. When XML generates, it often contains white-spaces between the nodes. The DOM parser treats these as ordinary elements, and if you are not aware of them, they sometimes cause problems.