How can I access an element of type <game: title> using simplexml?

I am trying to parse an XML feed using SimpleXML, here is a snippet of the XML tree:

<item>
<game:name>Tetris</game:name> 
<game:desc>Way Cool Game</game:desc>
<size>5mb</size> 
</item>

      

Actually, I can successfully access the "size" with something like this: $item->size

but how do I get the value? Of course, I can't call that:, $item->game:name

and I don't know how the call goes after: '. Is it a parameter, an attribute, or what?

Thanks for the advance!

+2


source to share


3 answers


You need to define a namespace for the game. XML requires all namespaces to be defined. There have been several previous answers about using custom namespaces: i.e.



+1


source


Use the children () function to get the children of the namespace.

$useThis = $xmlDoc->children("http://game.namespace/");
      

given that http: //game.namespace is the url of your games namespace in the root node.



Here's an explanation / sample:

+2


source


As for getting the SimpleXMLElement value, you just apply it to the string:

$size_value = (string)$item->size;

      

0


source







All Articles