There are a number of ways to parse, create and save XML in PHP. Two of the more common methods used are the SimpleXML library, and the DOMDocument class.
SimpleXML is extremely, well... Simple. With SimpleXML we can parse an XML document and easily process its elements, attributes, children and values.
Consider the following well formed, valid XML document.
]> A Christmas Carol Charles Dickens Fiction 1843 86 A Treatise on Government Aristotle Non-Fiction 322 195 A Thief in the Night E. W. Hornung Short Stories 1905 182 Some Book &unknown; Non-Fiction 500 1000
Parsing this document is as simple as creating a SimpleXML object with the file as a parameter, and looping through it's elements with a foreach loop.
$simple_xml = simplexml_load_file('valid.xml');
foreach($simple_xml as $book){
echo "Title: " . $book->title;
echo "Author: " . $book->author;
echo "Genre: " . $book->author;
echo "Year: " . $book->year;
// Get the Attribute
echo "Era: " . $book->year->attributes()->era;
echo "Pages: " . $book->pages;
echo "Link: " . $book->link;
echo "************************;
}
Note that SimpleXML objects can also be created the Object Oriented way by instantiating an object and passing the XML file path into it's contructor:
$simple_xml = new SimpleXMLElement('valid.xml', NULL, true);
As we loop through each book element, we can access its child elements by name. We can also access a child element's attributes by name, in this example, we access the 'era' attribute of the 'year' child element.
For more information on XML elements, attributes and entities, read
XML Fundamentals: Elements, Attributes and Entities
SimpleXML is great for quick and easy parsing, but what if our parsing needs are a bit more complex. Let's say for example that we were parsing XML from a web service and did not know the names of the child elements. Let's also say that we need to add a child element as we parse it's parent. We can accomplish both of these tasks using DomDocument.
The DomDocument is a robust class that allows us to add, modify, and even search for elements (XPATH queries).
$dom_xml = new DOMDocument();
$dom_xml->load('valid.xml');
$books = $dom_xml->getElementsByTagName('book');
foreach($books as $book){
//add new element
$date = $dom_xml->createElement("time-stamp");
$date->appendChild($dom_xml->createTextNode(time()));
$book->appendChild($date);
foreach($book->childNodes as $node){
if($node->nodeName != '#text') //omit blank space
echo $node->nodeName . ": " .
$node->nodeValue;
}
echo "************************";
}
In this example, we obtain a collection of 'book' elements and loop through them. As we loop through each element we create a new element called $date, append a text node to it (the date), and then append the $date element to the parent $book. Next, we abstractly loop through each child of the current 'book' element and output the child's node-name and value.
There is much more that can be done with DOM parsing. For more information check out the PHP online manual's entry on DOM here.
W3 Schools also has a great collection of tutorials and information.
You might also be interested in





