PHP SimpleXML
Introduction to PHP SimpleXML
The SimpleXML extension provides a simple way to get the name and text of an XML element, as long as you know the layout of the XML document.
SimpleXML converts XML documents into SimpleXMLElement objects.
This object can be processed like other objects through normal property selectors and array iterators.
Tip: Compared with the DOM or Expat parser, SimpleXML only requires a few lines of code to read the text data in the element.
Installation
SimpleXML extension requires PHP 5 support.
Since PHP 5, SimpleXML functions are an integral part of the PHP core. No installation is required to use these functions.
PHP 5 SimpleXML Function
Function | Description |
---|---|
__construct( ) | Create a new SimpleXMLElement object. |
addAttribute() | Add an attribute to the SimpleXML element. |
addChild() | Add a child element to the SimpleXML element. |
asXML() | Formats data for a SimpleXML object in XML (version 1.0). |
attributes() | Returns the attributes and values of the XML tag. |
children() | Find the child nodes of the specified node. |
count() | Calculate the number of child nodes of the specified node. |
getDocNamespaces() | Returns the declared namespaces in the document. |
getName() | Returns the name of the XML tag referenced by the SimpleXML element. |
getNamespaces() | Returns the namespaces used in the document. |
registerXPathNamespace() | Creates a namespace context for the next XPath query. |
saveXML() | Alias for asXML(). |
simplexml_import_dom() | Returns a SimpleXMLElement object from a DOM node. |
simplexml_load_file() | Convert XML file to SimpleXMLElement object. |
simplexml_load_string() | Convert XML string to SimpleXMLElement object. |
xpath() | Run an XPath query on XML data. |
PHP 5 SimpleXML iteration function
Function | Description |
---|---|
current() | Return 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() | Move to the next element. |
rewind() | Rewind to the first element. |
valid() | Check whether the current element is valid. |