I converting a XML-File into an Object using simplexml_load_file. I noticed a problem when a redunant element has a empty value.
I think this example make it more understandable:
// XML-File (Just a small excerpt look at "...")
...
<Team uID="t684">
...
<Player loan="" uID="p20388">
<Name>Manuel Neuer</Name>
<Position>Goalkeeper</Position>
<Stat Type="first_name">Manuel</Stat>
<Stat Type="last_name">Neuer</Stat>
<Stat Type="middle_name"></Stat>
<Stat Type="known_name"></Stat>
<Stat Type="birth_date">1986-03-27</Stat>
<Stat Type="birth_place"></Stat>
<Stat Type="first_nationality"></Stat>
<Stat Type="deceased"></Stat>
<Stat Type="preferred_foot"></Stat>
<Stat Type="weight">92</Stat>
<Stat Type="height">193</Stat>
<Stat Type="jersey_num">1</Stat>
<Stat Type="real_position">Goalkeeper</Stat>
<Stat Type="real_position_side">Unknown</Stat>
<Stat Type="join_date">2011-07-01</Stat>
<Stat Type="country">Germany</Stat>
</Player>
...
</Team>
...
// print_r (simplexml_load_file)
...
[Player] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[loan] =>
[uID] => p20388
)
[Name] => Manuel Neuer
[Position] => Goalkeeper
[Stat] => Array(
[0] => Manuel
[1] => Neuer
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Type] => middle_name
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Type] => known_name
)
)
[4] => 1986-03-27
[5] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Type] => birth_place
)
)
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Type] => first_nationality
)
)
[7] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Type] => deceased
)
)
[8] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Type] => preferred_foot
)
)
[9] => 92
[10] => 193
[11] => 1
[12] => Goalkeeper
[13] => Unknown
[14] => 2011-07-01
[15] => Germany
)
)
It would be the best when the "Type"-name were used as Array Key so I don't have to count on the order in the xml file. But at least a empty xml element value should also be a empty value in the array.
e.g.
<Stat Type="middle_name"></Stat>
should be
[2] =>
instead
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Type] => middle_name
)
)
I can work with these problems by:
- Count on steady order in XML (numeric index)
- Proof if value is from type SimpleXMLElement to determine if it's empty.
But that doesn't look like a good solution for me.
Am I doing something wrong or any ideas what I can do?
Many thanks
print_r
the information presented to you is not the real information. It's only informative with simplexml. Instead base your cognition on the fact that SimpleXML has much Magic and you best dump it's content by using theasXML()
method it offers, notprint_r
orvar_dump
. I bet this makes already 80% of your questions marks obsolete. – hakre Jul 1 '13 at 12:12$element[0]
is a so called self-reference, so$element[1][0]
is the self-reference of the second element in that list. Puzzled? I guess so. – hakre Jul 1 '13 at 12:48