PHP SimpleXML
PHP SimpleXML handles the most common XML tasks and leaves the rest for other extensions.
What is PHP SimpleXML?
SimpleXML is new in PHP 5.
The SimpleXML extension provides is a simple way of getting an XML element's name and text.
Compared to DOM or the Expat parser, SimpleXML just takes a few lines of code to read text data from an XML element.
SimpleXML converts the XML document (or XML string) into an object, like this:
- Elements are converted to single attributes of the SimpleXMLElement object. When there's more than one element on one level, they are placed inside an array
- Attributes are accessed using associative arrays, where an index corresponds to the attribute name
- Text inside elements is converted to strings. If an element has more than one text node, they will be arranged in the order they are found
SimpleXML is fast and easy to use when performing tasks like:
- Reading/Extracting data from XML files/strings
- Editing text nodes or attributes
However, when dealing with advanced XML, you are better off using the Expat parser or the XML DOM.
Installation
As of PHP 5, the SimpleXML functions are part of the PHP core. No installation is required to use these functions.
PHP SimpleXML Examples
Assume we have the following XML file, "note.xml":
<?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>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Now we want to output different information from the XML file above:
Example 1
Output keys and elements of the $xml variable (which is a SimpleXMLElement object):
<?php
$xml=simplexml_load_file("note.xml");
print_r($xml);
?>
$xml=simplexml_load_file("note.xml");
print_r($xml);
?>
Run example »
The output of the code above will be:
SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don't forget me this weekend! )
Example 2
Output the data from each element in the XML file:
<?php
$xml=simplexml_load_file("note.xml");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
$xml=simplexml_load_file("note.xml");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
Run example »
The output of the code above will be:
Tove
Jani
Reminder
Don't forget me this weekend!
Jani
Reminder
Don't forget me this weekend!
Example 3
Output the element's name and data for each child node:
<?php
$xml=simplexml_load_file("note.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
$xml=simplexml_load_file("note.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
Run example »
The output of the code above will be:
note
to: Tove
from: Jani
heading: Reminder
body: Don't forget me this weekend!
to: Tove
from: Jani
heading: Reminder
body: Don't forget me this weekend!
More PHP SimpleXML
For more information about the PHP SimpleXML functions, visit our PHP SimpleXML Reference.
0 comments:
Post a Comment