Parse XML with Namespace using SimpleXML
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'));}
You have to register the namespace for each simpleXMLElement object you use.
$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'));}
The namespace should also be declared somewhere in the xml file.
<event:event xmlns:event="http://www.webex.com/schemas/2002/06/service/event">...
The method ax described works too. You can skip the registerXPathNamespace if you know the xml file will always use the same prefix.
here alternative that worked for me.
$xml = simplexml_load_string($r);$ns = $xml->getNamespaces(true);foreach ($xml->children($ns['event'])->event as $skey) { $sessionKey = $skey->children($ns['event'])->sessionKey; echo $sessionKey;}