Home >Backend Development >PHP Tutorial >How to Parse XML with Namespaces Using SimpleXML?
Parsing XML with Namespace Using SimpleXML
XML namespaces can pose challenges when parsing XML documents. This question addresses the issue of parsing XML with a namespace using SimpleXML.
The provided XML document contains elements within the "event" namespace. To loop through the "event:event" nodes and retrieve specific elements like "event:sessionKey," the following code can be used:
$xml = new SimpleXMLElement($r); foreach($xml->xpath('//event:event') as $event) { var_export($event->xpath('event:sessionKey')); }
Note that the registerXPathNamespace function is not necessary in this case. The full namespace prefix can be used directly in the XPath queries. This modified code will display the values of all "event:sessionKey" elements within the document.
The above is the detailed content of How to Parse XML with Namespaces Using SimpleXML?. For more information, please follow other related articles on the PHP Chinese website!