PHP SimpleXML Functions

Beginner
5 min read
Updated: Nov 2026
24 function guides
Function index

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().

Beginner path.

Start with simplexml_load_string()—then learn attributes(), children() and xpath(), and finish with addChild(), addAttribute() and asXML().

Three loaders, many methods.

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:

PHP
$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 functions

Turn XML text, a file, or a DOM node into a SimpleXMLElement. The load functions return false on bad XML; the constructor throws an exception.

FunctionDescriptionTutorial
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 functions

Find out what an element is called, what it contains, and what it carries. Read child elements and attributes, count siblings, and get the text.

FunctionDescriptionTutorial
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 functions

List the namespaces a document uses, register a prefix for XPath, and search the tree with a query.

FunctionDescriptionTutorial
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 functions

Change the tree in memory, then turn it back into XML text or save it to a file.

FunctionDescriptionTutorial
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 functions

Methods of SimpleXMLIterator, which implements RecursiveIterator. foreach and RecursiveIteratorIterator call them for you.

FunctionDescriptionTutorial
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

They are the functions and methods of the SimpleXML extension. Three functions load XML into a SimpleXMLElement (simplexml_load_string(), simplexml_load_file(), simplexml_import_dom()), and the rest are methods of that element, such as children(), attributes(), xpath(), addChild(), and asXML().
The SimpleXML page teaches the ideas: loading, reading properties and attributes, querying with XPath, building XML, casting to string, and how SimpleXML compares with the XML Parser and DOM, with three fixed examples. This index lists every function tutorial so you can open simplexml_load_string(), xpath(), asXML(), and the rest.
Start with simplexml_load_string() or simplexml_load_file(), then learn attributes() and children() to read data. After that, learn xpath() to search, and addChild(), addAttribute() and asXML() when you need to build or save XML.
None. saveXML() is an alias of asXML(). With no argument both return the XML as a string, and with a file name both write the file and return true or false. Called on a child element they return only that element; called on the root they also add the XML declaration.
They belong to SimpleXMLIterator, a subclass of SimpleXMLElement that implements the RecursiveIterator interface, together with hasChildren() and getChildren(). You rarely call them yourself: foreach and RecursiveIteratorIterator call them for you. They are listed here so the whole SimpleXML family is in one place.
The SimpleXML extension may be disabled in your PHP build. Run var_dump(extension_loaded('simplexml')); to check. If it prints false, install the XML package for your PHP version (often called php-xml) or build PHP without --disable-simplexml, then restart PHP.

Start with SimpleXMLElement::__construct()

Open the next guide—syntax, examples, and FAQs.

__construct() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful