Latest web development tutorials

PHP XML DOM

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


What is the DOM?

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

W3C DOM is separated into different parts (Core, XML and HTML) and different levels (DOM Level 1/2/3):

* Core DOM - any structured document defines a standard set of objects
* XML DOM - XML ​​document defines a standard set of objects
* HTML DOM - defines a standard for HTML documents set of objects

To learn more about the knowledge of the XML DOM, visit our XML 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 parser: This parser transforms 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).
  • Time-based parser: The XML document as a series of events. When a specific event occurs, the parser will call the function to process.

DOM parser is a tree-based parser.

Look at the following XML document fragment:

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

XML DOM The XML above as a tree structure:

  • Level 1: XML document
  • Level 2: the root element: <from>
  • Level 3: text element: "Jani"

installation

DOM XML parser functions are part of the PHP core. No installation needed to use these functions.


XML file

The XML file below will be used in our example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


Loading and output XML

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

Examples

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

print $xmlDoc->saveXML();
?>

The code above will output:

ToveJaniReminder Don't forget me this weekend!

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

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

The example above creates a DOMDocument-Object, and the "note.xml" Loading the document in the XML object.

saveXML () function of the internal XML document into a string, so we can export it.


Traversing XML

We want to initialize the XML parser to load XML, and traverse all the elements <note> element:

Examples

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

$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
print $item->nodeName . " = " . $item->nodeValue . "<br>";
}
?>

The code above will output:

#text =
to = Tove
#text =
from = Jani
#text =
heading = Reminder
#text =
body = Don't forget me this weekend!
#text =

In the example above, you see the presence of air between each element of the text node.

When XML is generated, it will usually contain blanks between the nodes. XML DOM parser them as common elements, if you pay attention to them, sometimes cause problems.


To learn more about the knowledge of the XML DOM, visit our XML DOM tutorial .