I am loading an XML string using simplexml_load_string and if the node doesn’t have any child nodes of it’s own then the attributes of that node don’t seem to be mapped to the array correctly. Is there any way around this? Please see the CUSTOM_PROPERTY sections below:
Suppose I have the following XML:
<WEBSITE NAME="www.example.co.uk">
<CATEGORY ID="35702" NAME="CatName" FILE_NAME="" LONG_DESC="" SHORT_DESC="">
<CUSTOM>
<CUSTOM_PROPERTY NAME="CATTYPE_INDEX">6</CUSTOM_PROPERTY>
<CUSTOM_PROPERTY NAME="TEMPLATE_ID">0</CUSTOM_PROPERTY>
<CUSTOM_PROPERTY NAME="DISPLAY_LIMIT">10</CUSTOM_PROPERTY>
<CUSTOM_PROPERTY NAME="HIDE_ON_MENU">0</CUSTOM_PROPERTY>
<CUSTOM_PROPERTY NAME="CAT_COLOUR">#01b2a8</CUSTOM_PROPERTY>
</CUSTOM>
</CATEGORY>
When I use the following code:
$Xml = simplexml_load_string($Str);
print_r($Xml);
It returns this:
SimpleXMLElement Object
(
[@attributes] => Array
(
[NAME] => www.example.co.uk
)
[CATEGORY] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ID] => 35702
[NAME] => CatName
[FILE_NAME] =>
[LONG_DESC] =>
[SHORT_DESC] =>
)
[CUSTOM] => SimpleXMLElement Object
(
[CUSTOM_PROPERTY] => Array
(
[0] => 6
[1] => 0
[2] => 10
[3] => 0
[4] => #01b2a8
)
)
)
)
)
CUSTOM_PROPERTY should have an attribute NAME mapped but it doesn’t.
Your attributes are there but are not shown by
print_r(). Be aware thatprint_r()is not a reliable way of viewing the entire SimpleXML structure.Example:
Demo
Outputs