Nov
25
2008

XML: SimpleXML

SimpleXML is a PHP extension that allows users to easily manipulate/use XML data, PHP 5+ is required.

SimpleXML sacrifices ability to meet complex requirements in favour of providing simplified interface, aimed at ease of reading and iterating xml data. All PHP XML is based on same libarary therefore you can use simplexml and other PHP XML library’s together.

Parsing XML Documents
XML parsing is done by SimpleXML (useing DOM parsing model), the main things to note before using SimpleXML is that it supports XML 1.0 documents, if XML 1.1 is used you will receive an error, if the XML document is not well-formed it will also cause an error.

All objects created by SimpleXML are instances of SimpleXMLElement class (we are talking about OOP again :/ ) you have to create an instance of SimpleXMLElement; you can do this in many ways such as:
simplexml_load_strong(); Loads XML from string
simplexml_load_file(); Loads XML from a path
file_get_contents(); or curl can also be used to feed simplexml_load_strong(); but simplexml_load_file(); would be more sensible.

SimpleXML Functions
Their are 14 functions listed, we have gone trough two above .
http://php.net/manual/en/ref.simplexml.php

Loading XML
In the two examples bellow, we will use file_get_contents(); to get the contents of a xml document in a string, then parse the document useing simplexml.

// Load an XML string
$xmlstr = file_get_contents(’library.xml’);
$library = simplexml_load_string($xmlstr);
 
// Load an XML string OOP
$xmlstr = file_get_contents(’library.xml’);
$library = new SimpleXMLElement($xmlstr);

In the first example we use filegetcontents then parsing the XML document from a string using simplexml. Considering that SimpleXML is OOP the second does the exact same, except interacts directly with the Class instead of using interface simplexml_load_string();

In this example, we wont be loading XML into a string, as this would create overheads? Also their is no need to use filegetcontents as SimpleXML has the ability to load the file directly. Similar to the above examples they all do the same, the first one bellow uses the interface provided by SimpleXML where the second example interacts directly with the OOP Class.

// Load an XML file
$library = simplexml_load_file(’library.xml’);
 
// Load an XML file
$library = new SimpleXMLElement(’library.xml’, NULL, true);

You will also notice in the last example that we pass two perimeters, the first perimeter is asking specify additional libxml parameters that influence the way the library parses the XML for simplicity we use NULL. The second perimeter is used to tell the OOP language that we are not passing a string (FALSE), instead passing a file location (TRUE).

Example Loaded XML: library.xml
This is the contents of library.xml so we can manipulate it:

<?xml version="1.0"?>
<library>
<book isbn="031219126X">
<title>Frankenstein</title>
<author>M. Shelley</author>
<publisher>Bedford</publisher>
</book>
<book isbn="0312863551">
<title>The Moon Is a Harsh Mistress</title>
<author>R. A. Heinlein</author>
<publisher>Orb</publisher>
</book>
</library>

Think of the CHildren! (Accessing Children and Attributes)
We loaded XML four ways above! So now we know we have a SimpleXMLElmenet Object no matter what method we used.

Since SimpleXML is simple, PHP allows us to use arrays to receive all the data, its as easy as foreach:

$library = new SimpleXMLElement(’library.xml’, NULL, true);
foreach ($library->book as $book){
echo $book[’isbn’] . "\n";
echo $book->title . "\n";
echo $book->author . "\n";
echo $book->publisher . "\n\n";
}

As you can see we load the external XML. In the foreach we use $libary->book this is because the XML is stored in $libarary and book is the XML Element which holds the data. So for each element (book) we want its data in $book.
The first peace of data we need is the ISBN number, this is an attribute with its data inside the element deceleration (as you can see above) in this case we use $book['isbn']; the other data elements are within the element as you can see we use $book->author;

The drawback is you have to know the names of every element, but you should know the names! unless the provider change the structure. Luckily for PHP5 allows us to do this without knowing the names!

foreach ($library->children() as $child){
	echo $child->getName() . ":\n";
 
	// Get attributes of this element
	foreach ($child->attributes() as $attr){
		echo ’ ’ . $attr->getName() .:. $attr . "\n";
	}
 
	// Get children
	foreach ($child->children() as $subchild){
		echo ’ ’ . $subchild->getName() .:. $subchild . "\n";
	}
	echo "\n";
}

As you can see this uses SimpleXMLElement::children() and SimpleXMLElement::attributes(), as well as SimpleXMLElement::getName() to receive data without knowing the name. Vulnerability is that you don’t know what the data is saying, it could be nothing to do with the service you are providing. Personally I see limited use of these functions.

More Tutorials:
http://www.devshed.com/c/a/XML/SimpleXML/

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 06. XML and Web Services |

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment

WordPress Powered, Theme by TheBuckmaker.com | Add to Technorati Favorites. | RSS and Comments RSS