PHP XML DOM
The built-in DOM parser makes it possible to process XML documents in PHP.
What is DOM?
W3C DOM provides a standard set of objects for HTML and XML documents, as well as standard interfaces for accessing and manipulating these documents.
W3C DOM is divided into different parts (Core, XML, and HTML) and different levels (DOM Level 1/2/3):
- Core DOM - Defines a standard set of objects for any structured document
- XML DOM - Defines a standard set of objects for XML documents
- HTML DOM - Defines a standard set of objects for HTML documents
To learn more about XML DOM, please visit ourXML DOM Tutorial。
XML Parsing
To read and update - create and process - an XML document, you need an XML parser.
There are two basic types of XML parsers:
- Tree-based parsers: This type of parser converts an XML document into a tree structure. It analyzes the entire document and provides access to the elements in the tree, such as the Document Object Model (DOM).
- Event-based parsers: Treat an XML document as a series of events. When a specific event occurs, the parser calls a function to handle it.
The DOM parser is a tree-based parser.
Look at the following XML document fragment:
<from>Jani</from>
XML DOM treats the above XML as a tree structure:
- Level 1: XML document
- Level 2: Root element: <from>
- Level 3: Text element: "Jani"
Installation
DOM XML parser functions are part of the PHP core. No installation is required to use these functions.
XML File
The following XML file will be used in our examples:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Load and Output XML
We need to initialize the XML parser, load the XML, and output it:
Example
$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");
print $xmlDoc->saveXML();
?>
The above code will output:
If you view the source code in the browser window, you will see the following HTML:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The above example creates a DOMDocument-Object and loads the XML from "note.xml" into this document object.
The saveXML() function puts the internal XML document into a string, so we can output it.
Traverse XML
We will initialize the XML parser, load the XML, and traverse all elements of the <note> element:
Example
$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
print $item->nodeName . " = " . $item->nodeValue . "<br>";
}
?>
The above code will output:
to = Tove
#text =
from = Jani
#text =
heading = Reminder
#text =
body = Don't forget me this weekend!
#text =
In the above example, you see that there are empty text nodes between each element.
When XML is generated, it usually contains whitespace between nodes. The XML DOM parser treats them as ordinary elements, which can sometimes cause problems if you don't pay attention to them.
To learn more about XML DOM, please visit ourXML DOM Tutorial。
Other Extensions