Parse XML with Namespace using SimpleXML

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

I have this as xml:


    
        &         


        
相关标签:
6条回答
  • 2020-11-22 00:28

    Using registerXPathNamespace and then calling xpath didn't actually work for me. I had to go with the solution provided in this great post : http://blog.preinheimer.com/index.php?/archives/172-SimpleXML,-Namespaces-Hair-loss.html

    So in your case, this :

    echo $xml->children('http://www.webex.com/schemas/2002/06/service/event')->sessionName;
    

    Will output:

    Learn QB in Minutes

    0 讨论(0)
  • 2020-11-22 00:38

    Having worked a lot with simplexml, this is how I do it.

    The magic trick if you already have an element and just want to get it's different namespaced children, say for a structure like this:

    <entry>
    <title type="text">My test entry</title>
    <gd:when startTime="2017-02-26T02:00:00Z" endTime="2017-02-26T03:00:00Z"/>
    <gc:notes type="string">A type</gc:notes>
    </entry>
    

    Is to send TRUE as the second parameter to the children function:

      $title = (string) $entry->title;
      $gd = $entry->children('gd', TRUE);
      $attrs = $gd->when->attributes();
      $startTime = (string) $attrs->startTime;
      $gc = $entry->children('gc', TRUE);
      $notes = (string) $gc->notes();
    
    0 讨论(0)
  • 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.

    <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.

    0 讨论(0)
  • 2020-11-22 00:47

    Another approach is to use SimpleXML for parsing and DOMDocument for manipulation/access, which bypasses namespacing issues altogether:

    $xml = new SimpleXMLElement($r);
    $xml = dom_import_simplexml($xml);
    $nodelist= $xml->getElementsByTagName('event');  
    for($i = 0; $i < $nodelist->length; $i++) {
        $sessions = $nodelist->item($i)->getElementsByTagName('sessionKey');
        echo $sessions->item(0)->nodeValue;
    }
    
    0 讨论(0)
  • 2020-11-22 00:49

    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'));
    }
    
    0 讨论(0)
  • 2020-11-22 00:49

    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;
    }
    
    0 讨论(0)
提交回复
热议问题