WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP XML DOM Parser


The built-in DOM parser makes it possible to work with XML Documentation in PHP.


What is the DOM?

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

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 documentation
  • XML DOM-defines a standard set of objects for XML Documentation
  • HTML DOM-Define a standard set of objects for HTML Documentation

To learn more about XML DOM, please visit our XML DOM Tutorial.


XML parsing

To read and update-create and process-an XML Documentation, you need an XML parser.

There are two basic types of XML parsers:

  • Tree-based parser: This parser transforms XML Documentation into a tree structure. It analyzes the entire documentation and provides access to elements in the tree, such as the Documentation Object Model (DOM).
  • Event-based parser: Think of XML Documentation as a series of events. When a specific event occurs, the parser will call a function to handle it.

The DOM parser is a tree-based parser.

See the following XML Documentation snippet:

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

XML DOM Think of the XML above as a tree structure:

  • Level 1: XML Documentation
  • Level 2: Root element: <from>
  • Level 3: Text element: "Jani"

Installation

DOM XML parser functions are a core part of PHP. No installation is required to use these functions.


XML file

The following XML file 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 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 above code will output:

ToveJaniReminder Don't forget me this weekend!

If you view the source code in a 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 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.


Traversing XML

We want to initialize the XML parser, load the XML, and iterate <note> All elements of the 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 above code 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 saw 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.