Parsing XML with PHP's simpleXML

前端 未结 5 1449
北恋
北恋 2020-12-11 10:23

I\'m learning how to parse XML with PHP\'s simple XML. My code is:



        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 11:05

    Your xml contains a default namespace. In order to get your xpath query to work you need to register this namespace, and use the namespace prefix on every xpath element you are querying (as long as these elements all fall under the same namespace, which they do in your example):

    $xml = new SimpleXMLElement( $xmlSource );
    
    // register the namespace with some prefix, in this case 'a'
    $xml->registerXPathNamespace( 'a', 'http://www.apple.com/itms/' );
    
    // then use this prefix 'a:' for every node you are querying
    $results = $xml->xpath( '/a:Document/a:iTunes' );
    
    foreach( $results as $result )
    {
        echo $result . PHP_EOL; 
    }
    

提交回复
热议问题