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.
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.
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.
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/>";
}
}
}
}
?>
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