PHP DOM Parser

by Vincy. Last modified on July 14th, 2022.

DOM is a tree-based parser, used as an API to manipulate XML in PHP. Before PHP 5, we should add the domxml extension to use DOM parsers. From PHP 5, the libxml extension is used.

Since libxml is available as part of PHP core, we need not install or enable anything explicitly to use the DOM parser.

DOM Parser Features

PHP Dom parser provides many features. This API includes several classes with a list of properties, functions, and constructs to provide such features over XML documents.

  • DOM API includes classes for representing nodes based on their data. For example, DOMCharacterData, and DOMComment classes are used to represent character data nodes and comments nodes respectively.
  • DOMDocument class represents entire XML root. It includes a huge list of functions commonly suitable for all nodes like,
    • Loading XML file or document using XML parser.
    • Creating a new comment node, character data node, PI node
    • adding attributes to a node
    • accessing XML nodes by their ID, Name, and Namespace
    • saving XML document into a file or variable
    • validating XML document
  • It provides an interface to trace exceptions if any.
  • DOM API evaluates XPath expressions.
  • PHP DOM Parser supports utf8 character encoding.

dom_import_simplexml()

Apart from the above features DOM parser can interoperate with SimpleXML parsers using dom_import_simplexml() function. It requires a SimpleXML object as its argument and returns DOM formatted document as a result.

Example of parsing XML using DOM Parser

First, we should load this document to work with the DOM object. Using this object, we can iterate through the XML nodes to get their name and value pair.

<?php
$xmlDocument = '<?xml version="1.0"?>
<toys>
<toy>
<name>Ben 10 Watch</name>
<type>Battery Toys</type>
</toy>
<toy>
<name>Angry Birds Gun</name>
<type>Mechanical Toys</type>
</toy>
</toys>';
?>

This PHP code appends a new element into the target XML using DOM functions.

<?php
$dom = new DOMDocument();
$dom->loadXML($xmlDocument);
$target = $dom->getElementsByTagName("toys")->item(0);
$new_node = $dom->createElement("toy");
$child1 = $dom->createElement("name", "Remote Car");
$child2 = $dom->createElement("type", "Remote Toys");
$new_node->appendChild($child1);
$new_node->appendChild($child2);
$target->appendChild($new_node);
$elements = $dom->documentElement;
if (! is_null($elements)) {
    foreach ($elements->childNodes as $element) {
        echo "<br/><" . $element->nodeName . ">";
        $nodes = $element->childNodes;
        if (! is_null($nodes)) {
            foreach ($nodes as $node) {
                echo $node->nodeName . ":" . $node->nodeValue . "<br/>";
            }
        }
    }
}
?>

Output:

This program will display,

<#text>
#text: 
name:Ben 10 Watch
#text: 
type:Battery Toys
#text: 

<#text>
#text: 
name:Angry Birds Gun
#text: 
type:Mechanical Toys
#text: 

<#text>
name:Remote Car
type:Remote Toys
Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page