i want to parse xml files.
Its for an importer, where you can define a configuration
My Problem is, that the xml parser (SimpleXml & Dom) did it inconstistence.
When i only have one child node – it will give me a simpleXML
<sizes>
<size>
<gpp> 5,00</gpp>
<gppcurrency>EUR</gppcurrency>
<npp> 5,00</npp>
<nppcurrency>EUR</nppcurrency>
<sp> 5,00</sp>
<spcurrency>EUR</spcurrency>
<stock>100</stock>
</size>
</sizes>
Will be
sizes [SimpleXmlElement]
=> size [SimpleXmlElement]
- gpp
- gppcurrency
...
BUT if i have multiple nodes
<sizes>
<size>
<gpp> 5,00</gpp>
<gppcurrency>EUR</gppcurrency>
<npp> 5,00</npp>
<nppcurrency>EUR</nppcurrency>
<sp> 5,00</sp>
<spcurrency>EUR</spcurrency>
<stock>100</stock>
</size>
<size>
<gpp> 5,00</gpp>
<gppcurrency>EUR</gppcurrency>
<npp> 5,00</npp>
<nppcurrency>EUR</nppcurrency>
<sp> 5,00</sp>
<spcurrency>EUR</spcurrency>
<stock>100</stock>
</size>
</sizes>
It will output
sizes [SimpleXmlElement]
=> size array
[0] [SimpleXmlElement]
- gpp
- gppcurrency
...
[1] [SimpleXmlElement]
- gpp
- gppcurrency
...
This is realy incosistent and may you can help me find an answer for this.
Thanks
2
Answers
I have found my solution to consistencly get an array.
Whether or not the internal pieces use an array does not matter. What is being returned is a
SimpleXMLElement
object (not a bare array) with getters that will return an iterable value in either case. The array you are seeing is how the value is stored within theSimpleXMLElement
, but it will handle converting a single child item to an iterable value when you access it.