PHP SimpleXML Functions

About This Index
This page is the PHP SimpleXML Functions index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.
Learn how loading, properties, attributes, XPath, and building XML fit together on /php/simple-xml. Use this page when you need a specific function such as simplexml_load_string(), xpath(), or asXML().
Start with simplexml_load_string()—then learn attributes(), children() and xpath(), and finish with addChild(), addAttribute() and asXML().
Only simplexml_load_string(), simplexml_load_file() and simplexml_import_dom() are plain functions. Everything else is a method called on a SimpleXMLElement, and the iterator helpers belong to SimpleXMLIterator.
How to Call a PHP SimpleXML Function
The loaders are global functions—call them directly. Every other entry is a method, so load the XML first and call it on the element you get back:
$xml = simplexml_load_string($string); // SimpleXMLElement|false
// $xml = simplexml_load_file('data.xml'); // SimpleXMLElement|false
// $xml = new SimpleXMLElement($string); // throws on bad XML
$name = $xml->getName(); // string
$kids = $xml->children(); // SimpleXMLElement
$attrs = $xml->attributes(); // SimpleXMLElement
$total = $xml->count(); // int
$text = (string) $xml->title; // string, via __toString()
$hits = $xml->xpath('//book[@lang="en"]'); // array|false|null
$item = $xml->addChild('book', 'PHP'); // SimpleXMLElement
$item->addAttribute('id', '3');
$out = $xml->asXML(); // string
$ok = $xml->asXML('out.xml'); // bool
$it = new SimpleXMLIterator($string); // iterator helpers
for ($it->rewind(); $it->valid(); $it->next()) {
echo $it->key(), ': ', $it->current(), PHP_EOL;
} PHP SimpleXML Functions Table
All function guides in this PHP tutorial series (24 tutorials in 5 categories).
Load & construct
4 functionsTurn XML text, a file, or a DOM node into a SimpleXMLElement. The load functions return false on bad XML; the constructor throws an exception.
| Function | Description | Tutorial |
|---|---|---|
SimpleXMLElement::__construct() | Create an element from an XML string, or from a file or URL, and throw on bad XML. | Try it |
simplexml_load_string() | Load XML from a string and return the root element, or false. | Try it |
simplexml_load_file() | Load XML from a file or URL and return the root element, or false. | Try it |
simplexml_import_dom() | Wrap a DOM node as a SimpleXMLElement that shares the same data. | Try it |
Read structure
5 functionsFind out what an element is called, what it contains, and what it carries. Read child elements and attributes, count siblings, and get the text.
| Function | Description | Tutorial |
|---|---|---|
SimpleXMLElement::getName() | Get the tag name of an element. | Try it |
SimpleXMLElement::children() | Get the child elements, optionally for one namespace. | Try it |
SimpleXMLElement::attributes() | Get the attributes of an element, optionally for one namespace. | Try it |
SimpleXMLElement::count() | Count the child elements of an element. | Try it |
SimpleXMLElement::__toString() | Get the text of an element as a string; used by (string) and echo. | Try it |
Namespaces & xpath
4 functionsList the namespaces a document uses, register a prefix for XPath, and search the tree with a query.
| Function | Description | Tutorial |
|---|---|---|
SimpleXMLElement::getNamespaces() | Get the namespaces used by an element and, optionally, its children. | Try it |
SimpleXMLElement::getDocNamespaces() | Get the namespaces declared in the document. | Try it |
SimpleXMLElement::registerXPathNamespace() | Register a prefix for a namespace URI to use in XPath queries. | Try it |
SimpleXMLElement::xpath() | Run an XPath query and return an array of matching elements. | Try it |
Write & export
4 functionsChange the tree in memory, then turn it back into XML text or save it to a file.
| Function | Description | Tutorial |
|---|---|---|
SimpleXMLElement::addChild() | Add a child element and return it. | Try it |
SimpleXMLElement::addAttribute() | Add an attribute to an element. | Try it |
SimpleXMLElement::asXML() | Return the element as an XML string, or write it to a file. | Try it |
SimpleXMLElement::saveXML() | Alias of asXML(): return the XML string or write a file. | Try it |
Iterator helpers
7 functionsMethods of SimpleXMLIterator, which implements RecursiveIterator. foreach and RecursiveIteratorIterator call them for you.
| Function | Description | Tutorial |
|---|---|---|
SimpleXMLIterator::rewind() | Move the iterator back to the first element. | Try it |
SimpleXMLIterator::current() | Get the element at the current position. | Try it |
SimpleXMLIterator::key() | Get the name of the current element. | Try it |
SimpleXMLIterator::next() | Move the iterator to the next element. | Try it |
SimpleXMLIterator::valid() | Check whether the current position holds an element. | Try it |
SimpleXMLIterator::hasChildren() | Check whether the current element has child elements. | Try it |
SimpleXMLIterator::getChildren() | Get an iterator over the children of the current element. | Try it |
Frequently Asked Questions
Start with SimpleXMLElement::__construct()
Open the next guide—syntax, examples, and FAQs.
7 people found this page helpful
