PHP XML DOM

The built-in DOM parser makes it possible to process XML documents in PHP.

What is DOM?

The W3C DOM provides a standard set of objects for HTML and XML documents, as well as standard interfaces for accessing and manipulating these documents.

The 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

If you want to learn more about XML DOM, please visit our XML DOM Tutorial.

XML Parsing

To read and update - create, create, and handle - an XML document, you need an XML parser.

There are two basic types of XML parsers:

  • Tree-based parserIt converts the XML document into a tree structure. It analyzes the entire document and provides an API to access elements in the tree, such as the Document Object Model (DOM).
  • Event-based parserIt treats the 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.

Please see the following fragment of the XML document below:

<?xml version="1.0" encoding="ISO-8859-1"?>
<from>John</from>

The XML DOM views XML as a tree structure:

  • Level 1: XML document
  • Level 2: Root element: <from>
  • Level 3: Text element: "John"

Installation

The DOM XML parser is a core part of PHP. These functions can be used without installation.

XML File

In our example, we will use the following XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

Loading and outputting XML

We need to initialize the XML parser, load the XML, and output it:

Example

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");
print $xmlDoc->saveXML();
?>

The output of the above code is:

George John Reminder Don't forget the meeting!

If you view the source code in your browser window, you will see the following HTML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

In the above example, a DOMDocument object is created, and the XML in "note.xml" is loaded into this document object.

The saveXML() function puts the internal XML document into a string so that we can output it.

Loop XML

We need to initialize the XML parser, load XML, and loop through all elements of the <note> element:

Example

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
  {
  print $item->nodeName . " = " . $item->nodeValue . "<br />";
  }
?>

The output of the above code is:

#text = 
to = George
#text = 
from = John
#text = 
heading = Reminder
#text = 
body = Don't forget the meeting!
#text =

In the above example, you saw empty text nodes between each element.

When XML is generated, it usually contains whitespace between nodes. XML DOM parser treats them as ordinary elements, and if you are not careful, they can sometimes cause problems.

If you want to learn more about XML DOM, please visit our XML DOM Tutorial.