PHP 5 SimpleXML Functions

Introduction to PHP SimpleXML


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.

With normal attribute selectors and array iterators, this object can be processed like any other object.

Tip: Compared to the DOM or Expat parser, SimpleXML requires only a few lines of code to read the text data in the element.


SimpleXML extension requires PHP 5 support.

Since PHP 5, SimpleXML functions are a core part of PHP. No installation is required to use these functions.

PHP 5 SimpleXML functions

function Description
__construct () Creates a new SimpleXMLElement object.
addAttribute () Add an attribute to the SimpleXML element.
addChild () Add a child element to the SimpleXML element.
asXML () Format data for SimpleXML objects in XML (version 1.0).
attributes () Returns the attributes and values ​​of the XML tag.
children () Find the children of the specified node.
count () Count the number of children of the specified node.
getDocNamespaces () Returns the declared namespace in the document.
getName() Returns the name of the XML tag referenced by the SimpleXML element.
getNamespaces () Returns the namespace used in the document.
registerXPathNamespace () Creates a namespace context for the next XPath query.
saveXML () asXML () alias.
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 XPath queries on XML data.

PHP 5 SimpleXML iteration function

function Description
current () Returns the current element.
getChildren () Returns the children of the current element.
hasChildren () Check if the current element has children.
key () Returns the current key.
next () Move to the next element.
rewind () Back to the first element.
valid () Check if the current element is valid.