PHP SimpleXML not really that simple

Swa

Honorary Master
Joined
May 4, 2012
Messages
37,773
Reaction score
11,070
Location
www
I've been wondering if they could possibly make this (and PHP) any more complicated. For the life of me I can't find a way.

First problem I run into is that SimpleXML expects a single XML element to encapsulate the document. Having multiple elements results in it falling over with only a blank screen. Not even an error message saying where the problem is. Ok well, after figuring that out through a wild guess let's move on. I'll use an example document like this
<xml>
<value attribute="random attribute">random string</value>
<value>random number</value>
</xml>

Should be simple right? var_dump() and print_r() both output SimpleXMLElement Object ( [value] => Array ( [0] => random string [1] => random number ))

Ok so we can test for multiple elements using is_array()... Wrong, because it turns out "value" is not actually an array as both var_dump() and print_r() incorrectly report. "value" is a property of the SimpleXMLElement object and not an array element as you'd expect. Which is why $xml['value'] fails and you have to use $xml->value instead. Strangely $xml->value['attribute'] does work though. This is probably why it can't handle XML documents in multiple segment as it's all contained in a single object.

Now with that out of the way let's continue. We've already established that $xml->value is not an array so passing it to print_r() does not give you the value of both properties. But because of how PHP handles object properties both can be accessed with $xml->value[0] and $xml->value[1] giving the further false impression they are part of an array. $xml->value is the same as $xml->value[0] and gives only the first property of an SimpleXMLElement with multiple properties of the same name and not an array of properties.

So smooth sailing it should be from now on. Until you get to using $xml->value. Again you'd think it's a string but passing it to $_POST[$xml->value[0]] gives nothing, because again the $xml-value[0] property turns out to not be a string but another SimpleXMLElement object. To get it to work correctly it has to be converted to a string with (string) or by calling $xml->value[0]->__toString() directly. Not a very elegant solution. One would think there'd at least be a property to access the value directly.

Which brings up another point. Casting $xml->value[1] to a string gives no indication that it's actually a number. Without extra formatting information or testing it there's no way to know what was intended. Probably because the extra complicated "simplexml" extensions has no way to detect data types and handles them all as strings. With the PHP parser being able to detect data types could they not have extended this into the xml parser and provided an easy to access property to retrieve the actual value?

I'm really starting to wonder who put this whole thing together...

/rant
 
And on the eighth day be realised his mistake in creating XML and gave his people JSON instead.
 
Agreed with the others. Do not use XML, unless you are forced to use it by some external dependency.
 
Top
Sign up to the MyBroadband newsletter
X