PHP DOM and XML: An Overview

Written by Tom on Monday 03/09/07


The DOM extension allows you to operate on XML documents through the DOM API with PHP 5.


It is fully featured and fully object orientated.


The basic functionality is demonstrated below. For the full documentation click here.


Example XML file.


<root>
<item name="people">
<field name="bob">
<age>50</age>
</field>
<field name="trev">
<age>40</age>
</field>
</item>
</root>


All tags in a XML file are paired tags.
There is always a root tag some times called root but can be called Susy if you like.
The file is made up of parent and child nodes. item is a parent of field. field is a parent of age... etc
item and field both have an attribute called name, you can have multiple attributes separated by a space.


Opening an XML file


$doc = new DOMDocument;
$xml = "../dataBase/myDatabase.xml";
$doc->Load($xml);


First you create a new DOMDocument object. Then you fill that object with the contents of an XML file via the Load function.


Saving to an XML file


$doc->save($xml)- Dumps the internal XML tree back into a file creating an XML document from the DOM representation.

$doc->saveXML()- Dumps the internal XML tree back into a string

$doc->saveXML('field')- Output only a specific node without XML declaration rather than the entire document.


Processing the file


$fields = $doc->getElementsByTagName("field");


The getElementsByTagName is very handy and returns an object that contains every instance of, in this case the field node.


We can now iterate through the $fields object



function findField($fields,$fieldToFind){
foreach ($fields as $field) {
if($field->getAttribute("name") == $fieldToFind){
if($field->hasChildNodes()){
$children = $field->childNodes;
return $children;
}
}
}
return 0;
}


In the above example we look at the attribute of field node looking for the $fieldToFind. When a match is found we check to see if the node has any children via the hasChildNodes() method. If it does we return the children which are further processed.



function findChild($children,$child){
for($i=0;$i<$children->length;$i++){
$kid = $children->item($i);
if($kid->nodeName == $child){
return $kid;
}
}
return 0;
}


From the children object we can iterate through to find a particular child this is then passed back and data is read or written.
Note: We use the length attribute to control the for loop, and the item method to point to one child at a time.
The nodeName attribute is used to test to see if the correct child is selected.