PHP DOM Parser

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, libxml extension is used. Since libxml is available as part of PHP core, we need not install or enable anything explicitly to use DOM parser.

DOM Parser Features

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

  • DOM API includes classes for representing nodes based on their data. For example, DOMCharacterData, DOMComment classes are used to represent character data nodes, comments nodes respectively.
  • DOMDocument class represents entire XML root. It includes huge list of functions commonly suitable for all nodes like,
    • Loading XML file or document using XML parser.
    • Creating 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 interface to trace exceptions if any.
  • DOM API evaluates XPath expressions.
  • PHP DOM Parser supports utf8 character encoding.

parsing

dom_import_simplexml()

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

Example to Parse XML using DOM Parser

First, we should load this document to work with 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 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

This PHP code tutorial was published on November 17, 2013.

↑ Back to Top