I have this small chunk of XML that I wish to parse in PHP. The Works tag is ok, I can parse all the attributes just fine. The problem I'm having is with the Doesnt tag, it seems that because it has text content that I can't access the attributes.
<Export id="123" apples="pears">
<Works foo="bar" id="234"/>
<Doesnt bar="foo" id="345">Stack Exchange</Doesnt>
</Export>
I run the following (very simple) code:
$plain = '<Export id="123" apples="pear....esnt></Export>'; // as above
$sxe = simplexml_load_string($plain);
$json = json_encode($sxe);
$native = json_decode($json);
print_r($sxe, true);
print_r($native, true);
And I end up with the following output:
SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 123
[apples] => pears
)
[Works] => SimpleXMLElement Object
(
[@attributes] => Array
(
[foo] => bar
[id] => 234
)
)
[Doesnt] => Stack Exchange
)
stdClass Object
(
[@attributes] => stdClass Object
(
[id] => 123
[apples] => pears
)
[Works] => stdClass Object
(
[@attributes] => stdClass Object
(
[foo] => bar
[id] => 234
)
)
[Doesnt] => Stack Exchange
)
As you can see, the SimpleXMLElement object and the stdClass object are both missing all the attributes for the <Doesnt> tag. Is there some work around or alternative means to get at them?
As @Gordon said, you don't get the whole picture with print_r and var_dump: SimpleXMLElement Object has some unusual properties, so these dumping functions would fail to represent its structure correctly.
Still, if you had used this instead:
$sxe = simplexml_load_string($plain);
var_dump($sxe->Doesnt);
... you would have seen these attributes and their values intact.
But look closely at the output:
object(SimpleXMLElement)[3]
public '@attributes' =>
array (size=2)
'bar' => string 'foo' (length=3)
'id' => string '345' (length=3)
string 'Stack Exchange' (length=14)
It's kinda unusual, don't you think, to have string value just hanging up there - without any corresponding property owning it? But that's exactly what would make direct conversion problematic. JSON (as it is) deals with simple structures - objects and arrays - and this structure cannot be represented correctly in neither of them: you have at least to introduce some additional attribute to store the text content.
Still, this approach seems to be in demand (to some extent), and you're definitely not alone: here's an open ticket in PHP bug tracking system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With