SimpleXML Type Cheatsheet

A SimpleXMLElement can represent many different things, from an element, to a list of attributes or childelements.

Sometimes it’s good to know how to find out, especially with the magic the extension comes with. The following is a table with the different types and an expression for it, $element is the Simplexml element:

Type Test
Element $element->xpath('.') == array($element)
Attribute $element[0] == $element
and $element->xpath('.') != array($element)
Attributes $element->attributes() === NULL
Elements $element[0] != $element
and $element->attributes() !== NULL
Single $element[0] == $element
Empty List $element[0] == NULL
Document Element $element->xpath('/*') == array($element)

PHP Version Info: PHP 5.2.2 or higher needed.

Usage Example

Normaly dom_import_simplexml comes to the rescue to upgrade from SimpleXML to DOMDocument. It works for nearly any SimpleXMLElement object, but not for an empty attribute or element list. To prevent the warning, check:

$isEmpty = $element[0] == NULL;
$DOMNode = $isEmpty ? NULL : dom_import_simplexml($element);

Common Xpath Cheats

SimpleXML can be pretty limited, some of the missing features can be worked-around before upgrading to DOMDocument by making use of Xpath:

What Xpath Note
Parent Element .. Works also for attribute
Document Element /*

The SimpleXMLElement::xpath() method returns an array by default. The array can only contain element or attribute nodes, nothing else. On an empty list SimpleXMLElement the method returns NULL. A FALSE is returned in case the Xpath expression had an error. So you can safely cast to an array for streamlining. It returns an empty array if there is no result then.

Usage Example

Obtaining the document element which represents the SimpleXMLElement returned by new, simplexml_load_file() or simplexml_load_string(). For example if $element is an element somewhere in the XML but the full document should be output:

list($root) = $element->xpath('/*');
$root->asXML();

SimpleXML Line Separator

Is "\n" (LF, \x0A, 10), always. When the document element is output asXML() the first line is the XML declaration. The last line is empty (newline at the end of file).


See as well

How to tell apart SimpleXML objects representing element and attribute?
In SimpleXML, how can I add an existing SimpleXMLElement as a child element?
https://bugs.php.net/bug.php?id=52858

This entry was posted in Hakre's Tips, PHP Development, Pressed, The Know Your Language Department and tagged , , , , , , , , , . Bookmark the permalink.

One Response to SimpleXML Type Cheatsheet

  1. Pingback: SimpleXML and JSON Encode in PHP – Part I | hakre on wordpress

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s