1

I have the following XML output from my website:

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <status>0</status>
  <item>
    <message>OK</message>
    <id>123</id>
  </item>
</result>

I'd like to grab the value inside <id> & store in a variable.

Is there a way to do this without using SimpleXML?

halfer
  • 19,824
  • 17
  • 99
  • 186
michaelmcgurk
  • 6,367
  • 23
  • 94
  • 190

2 Answers2

2

There are a lot of ways to do this: See the official documentation:

XML Manipulation : http://docs.php.net/manual/en/refs.xml.php

The problem is they are all much much more complicated than SimpleXML and nowhere as fast for random access

I would go with simplexml like this:

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<result>
  <status>0</status>
  <item>
    <message>OK</message>
    <id>123</id>
  </item>
</result>';

$xmlcont = new SimpleXMLElement($xml);

echo $xmlcont->item->id;

Output : 123
NID
  • 3,238
  • 1
  • 17
  • 28
0

Refer This Using DOM

Using simplexml_load_file

AnuradhaS
  • 341
  • 2
  • 10