SimpleXML handles the most common XML tasks and leaves the rest for other extensions.
What is SimpleXML?
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:
- Elements - Are converted to single attributes of the SimpleXMLElement object. When there's more than one element on one level, they're placed inside an array
- Attributes - Are accessed using associative arrays, where an index corresponds to the attribute name
- Element Data - Text data from elements are 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 basic tasks like:
- Reading XML files
- Extracting data from XML strings
- Editing text nodes or attributes
However, when dealing with advanced XML, like namespaces, you are better off using the Expat parser or the XML DOM.
Installation
As of PHP 5.0, the SimpleXML functions are part of the PHP core. There is no installation needed to use these functions.
Using SimpleXML
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>
<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:
- Load the XML file
- Get the name of the first element
- Create a loop that will trigger on each child node, using the children() function
- Output the element name and data for each child node
Example
<?php
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
}
?>
$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!
to: Tove
from: Jani
heading: Reminder
body: Don't forget me this weekend!
PHP SimpleXML Functions
PHP: indicates the earliest version of PHP that supports the function.
Function | Description | PHP |
---|---|---|
__construct() | Creates a new SimpleXMLElement object | 5 |
addAttribute() | Adds an attribute to the SimpleXML element | 5 |
addChild() | Adds a child element the SimpleXML element | 5 |
asXML() | Gets an XML string from a SimpleXML element | 5 |
attributes() | Gets a SimpleXML element's attributes | 5 |
children() | Gets the children of a specified node | 5 |
getDocNamespaces() | Gets the namespaces of an XML document | 5 |
getName() | Gets the name of a SimpleXML element | 5 |
getNamespace() | Gets the namespaces from XML data | 5 |
registerXPathNamespace() | Creates a namespace context for the next XPath query | 5 |
simplexml_import_dom() | Gets a SimpleXMLElement object from a DOM node | 5 |
simplexml_load_file() | Gets a SimpleXMLElement object from an XML document | 5 |
simplexml_load_string() | Gets a SimpleXMLElement object from an XML string | 5 |
xpath() | Runs an XPath query on XML data | 5 |
PHP SimpleXML Constants
None
No comments:
Post a Comment