From http://www.w3schools.com (Copyright Refsnes Data)
SimpleXML handles the most common XML tasks and leaves the rest for other extensions.
SimpleXML is new in PHP 5. It is an easy way of getting an element's attributes and text, if you know the XML document's layout.
Compared to DOM or the Expat parser, SimpleXML just takes a few lines of code to read text data from an element.
SimpleXML converts the XML document into an object, like this:
SimpleXML is fast and easy to use when performing basic tasks like:
However, when dealing with advanced XML, like namespaces, you are better off using the Expat parser or the XML DOM.
As of PHP 5.0, the SimpleXML functions are part of the PHP core. There is no installation needed to use these functions.
Below is an XML file:
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> |
We want to output the element names and data from the XML file above.
Here's what to do:
Example
<?php $xml = simplexml_load_file("test.xml"); echo $xml->getName() . "<br />"; foreach($xml->children() as $child) { echo $child->getName() . ": " . $child . "<br />"; } ?> |
The output of the code above will be:
note to: Tove from: Jani heading: Reminder body: Don't forget me this weekend! |
For more information about the PHP SimpleXML functions, visit our
PHP SimpleXML Reference.
From http://www.w3schools.com (Copyright Refsnes Data)