PHP 5 SimpleXML Functions
PHP SimpleXML Introduction
The SimpleXML extension provides a simple way to get the names and text of XML elements, as long as you know the layout of the XML document.
SimpleXML converts an XML document into a SimpleXMLElement object.
Through normal property selectors and array iterators, this object can be handled like any other object.
**Tip:** Compared with DOM or the Expat parser, SimpleXML requires only a few lines of code to read the text data in elements.
Installation
The SimpleXML extension requires PHP 5 support.
Since PHP 5, SimpleXML functions are part of the PHP core. These functions can be used without installation.
PHP 5 SimpleXML Functions
| function | Description |
|---|---|
| __construct() | Creates a new SimpleXMLElement object. |
| addAttribute() | Adds an attribute to a SimpleXML element. |
| addChild() | Adds a child element to a SimpleXML element. |
| asXML() | Formats the data of a SimpleXML object as XML (version 1.0). |
| attributes() | Returns the attributes and values of an XML tag. |
| children() | Finds the child nodes of a specified node. |
| count() | Counts the number of child nodes of a specified node. |
| getDocNamespaces() | Returns the declared namespaces in the document. |
| getName() | Returns the name of the XML tag referenced by a SimpleXML element. |
| getNamespaces() | Returns the namespaces used in the document. |
| registerXPathNamespace() | Creates a namespace context for the next XPath query. |
| saveXML() | asXML() is an alias of. |
| simplexml_import_dom() | Returns a SimpleXMLElement object from a DOM node. |
| simplexml_load_file() | Converts an XML file into a SimpleXMLElement object. |
| simplexml_load_string() | Converts an XML string into a SimpleXMLElement object. |
| xpath() | Runs an XPath query against XML data. |
PHP 5 SimpleXML Iterator Functions
| function | Description |
|---|---|
| current() | Returns the current element. |
| getChildren() | Returns the child elements of the current element. |
| hasChildren() | Checks whether the current element has child elements. |
| key() | Returns the current key. |
| next() | Moves to the next element. |
| rewind() | Rewinds to the first element. |
| valid() | Checks whether the current element is valid. |