Home > Programming > The SimpleXML

The SimpleXML

The SimpleXML extensions is intended to provide a “very simple and easily usable toolset”. This description lets you think that is simple to use but a more accurate description is that it lets you use it for for quick and simple cases. The basic example looks good:

$movies = new SimpleXMLElement($xmlstr);
 
/* For each <character> node, we echo a separate <name>. */
foreach ($movies->movie->characters->character as $character) {
   echo $character->name, ' played by ', $character->actor, PHP_EOL;
}

One little issue: $character->name is an object not a regular php string. As a result, you have to cast everywhere as if you use a static language:

if((string) $foo->bar == 'etc') {

or

str_etc((string)$xml->str);

There are also small differences on what functions are available or how they work, depending on what version of PHP you use.

In case of a slightly more complex xml (like some custom data feed) it is an usual approach to convert the xml object to nested array structure. http://php.net/manual/en/book.simplexml.php has a section of user contributed notes that is full of post regarding this action. There are two basic approaches:

$array = json_decode(json_encode($xml), TRUE);
...
// some cleanup if neeed

and some recursive parsing of the xml tree. Both methods kind of work if applied for the right problem. This shows that for the more than basic tasks SimpleXML becomes cumbersome, a nuisance to use.

The extension could be improved a bit to make it more usable but I think a lot of problems come from the nature of the XML format. Most data feed providers do not actually need or use the power/complexity of the XML format and could use a lighter more suitable format. Used data from providers that could easily use csv or json. It is easier to generate, parse, debug and use a lot less bandwidth & processing power all at the cost of sounding less “enterprise”.

Categories: Programming Tags: , ,
  1. No comments yet.