Search code examples
phpxmlsimplexml

simplexml loop empty nodes entry for attributes


I have the following XML structure:

<parent>
    <child foo="bar1" foo2="bar2"/>
    <child foo="bar3" foo2="bar4"/>
</parent>

and in PHP:

foreach ($xml->parent->child as $item) 

returns "Invalid argument supplied for foreach()"

can I loop through those children with SimpleXML? and yes there is no space in at the end of the line near to /> .. is that a problem? I just get the data and I have to parse it....


Solution

  • $xml contains your root node <parent>, so do:

    foreach ($xml->child as $item) 
    

    the closing tag /> is correct.

    see https://eval.in/455645