Parse XML with Namespace using SimpleXML

前端 未结 6 958
独厮守ぢ
独厮守ぢ 2020-11-21 23:45

I have this as xml:


    
        &         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 00:45

    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.

    
    ...
    

    The method ax described works too. You can skip the registerXPathNamespace if you know the xml file will always use the same prefix.

提交回复
热议问题