0

I am trying to parse an XML file that has namespaces in it using simplexml_load_string in PHP. A sample of the xml response is here: http://pastie.org/4404714

Usually I would parse this using the simplexml_load_string command and then dump the results out to get a nice easy to read tree so that I can access the objects. However, all of the data does not even show up when I do this in this case. The segments below the AttributeSets don't show up at all. I have to output the raw xml to even see them.

I read in other similar stackoverflow answers that I could just ignore the namespace portion in the tags, but I can't get that to work either. Could someone please help. For example, how would I print out Author, Height, or Edition from this response. Thanks

user1452536
  • 103
  • 8
  • possible duplicate of [How do I parse XML containing custom namespaces using SimpleXML?](http://stackoverflow.com/questions/1133897/how-do-i-parse-xml-containing-custom-namespaces-using-simplexml) – vascowhite Aug 07 '12 at 09:18

2 Answers2

0

Maybe this question here on SO will help you.

Basically what the answer suggests is that you have to register the namespace for each simpleXMLElement object you use.

example from the linked question:

$xml = new SimpleXMLElement($r);
$xml->registerXPathNamespace('e', 'http://www.webex.com/schemas/2002/06/service/event');

foreach($xml->xpath('//e:event') as $event) {
    $event->registerXPathNamespace('e', 'http://www.webex.com/schemas/2002/06/service/event');
    var_export($event->xpath('//e:sessionKey'));
}

Or as another solution explains: it does work without registerXPathNamespace and the full namespace prefix in the xpath queries:

$xml = new SimpleXMLElement($r);

foreach($xml->xpath('//event:event') as $event) {
    var_export($event->xpath('event:sessionKey'));
}
Community
  • 1
  • 1
Nikola
  • 14,888
  • 21
  • 101
  • 165
  • I read that question and attempted the answer, but I can't figure out how to apply it to my situation. The layout of the XML in that example is different than mine in the sample I posted. Does anyone have any idea how to write that so that it will work with my sample xml? – user1452536 Aug 07 '12 at 21:24
0

After much research, I found two ways to solve. One is the correct way, and other is quick and dirty. This is the site which explains how to do it the right way. I was able to parse the string using simple_xml_load_string in PHP, then get the children of the object that contained the different namespace just like in the example on that page. That was a pain but doable.

Finally, since I just needed a simple, quick solution, I took the raw XML data and did a str_replace('ns2:','',$rawxml), then parsed it with simple_xml_load_string and it works like a charm. I just $call->it->like->this.

Some people will not like this solution, but namespaces are used to avoid duplicate fields, and because of the nature of the way I am parsing and using the data, this will not be an issue for me.

Nikola
  • 14,888
  • 21
  • 101
  • 165
user1452536
  • 103
  • 8