What @Yottatron offers is true, but not all cases, as this example shows:
if your XML looks like this:
<?xml version='1.0'?> <testing> <lol> <lolelem>Lol1</lolelem> <lolelem>Lol2</lolelem> <notlol>NotLol1</lolelem> <notlol>NotLol1</lolelem> </lol> </testing>
The output of Simplexml will be:
SimpleXMLElement Object ( [lol] => SimpleXMLElement Object ( [lolelem] => Array ( [0] => Lol1 [1] => Lol2 ) [notlol] => Array ( [0] => NotLol1 [1] => NotLol1 ) ) )
and writing
$xml->lol->lolelem
you expect your result to be
Array ( [0] => Lol1 [1] => Lol2 )
but instead you get:
SimpleXMLElement Object ( [0] => Lol1 )
and
$xml->lol->children()
You'll get:
SimpleXMLElement Object ( [lolelem] => Array ( [0] => Lol1 [1] => Lol2 ) [notlol] => Array ( [0] => NotLol1 [1] => NotLol1 ) )
What you need to do if you want only lolelema:
$xml->xpath("//lol/lolelem")
This gives this result (not as the expected form, but contains the correct elements)
Array ( [0] => SimpleXMLElement Object ( [0] => Lol1 ) [1] => SimpleXMLElement Object ( [0] => Lol2 ) )
Taha paksu
source share