Parsing XML with PHP's simpleXML

2019-01-20 04:26发布

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

<?php
$xmlSource = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>    <Document xmlns=\"http://www.apple.com/itms/\" artistId=\"329313804\" browsePath=\"/36/6407\" genreId=\"6507\">    <iTunes> myApp </iTunes> </Document>";

$xml = new SimpleXMLElement($xmlSource);

$results = $xml->xpath("/Document/iTunes");
foreach ($results as $result){
 echo $result.PHP_EOL;  
}

print_r($result);
?>

When this runs it returns a blank screen, with no errors. If I remove all the attributes from the Document tag, it returns :

myApp SimpleXMLElement Object ( [0] => myApp )

Which is the expected result.

What am I doing wrong? Note that I don't have control over the XML source, since it's coming from Apple.

5条回答
做个烂人
2楼-- · 2019-01-20 04:59

Seems if you use the wildcard (//) on xpath it will work. Also, not sure why but if you remove the namespace attribute (xmlns) from the Document element, your current code will work. Maybe because a prefix isn't defined? Anyway, following should work:

$results = $xml->xpath("//iTunes");
foreach ($results as $result){
 echo $result.PHP_EOL;  
}
查看更多
等我变得足够好
3楼-- · 2019-01-20 05:00

This line:

print_r($result);

is outside the foreach loop. Maybe you should try

print_r($results);

instead.

查看更多
祖国的老花朵
4楼-- · 2019-01-20 05:01

this is general example

foreach ($library->children() as $child)
{
   echo $child->getName() . ":\n";
   foreach ($child->attributes() as $attr)
   {
    echo  $attr->getName() . ': ' . $attr . "\n";
  }
foreach ($child->children() as $subchild)
{
    echo   $subchild->getName() . ': ' . $subchild . "\n";
}
   echo "\n";
}

for more information check this : http://www.yasha.co/XML/how-to-parse-xml-with-php-simplexml-DOM-Xpath/article-1.html

查看更多
何必那么认真
5楼-- · 2019-01-20 05:08

For the part about the default namespace, read fireeyedboy's answer. As mentionned, you need to register a namespace if you want to use XPath on nodes that are in the default namespace.

However, if you don't use xpath(), SimpleXML has its own magic that selects the default namespace automagically.

$xmlSource = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>    <Document xmlns=\"http://www.apple.com/itms/\" artistId=\"329313804\" browsePath=\"/36/6407\" genreId=\"6507\">    <iTunes> myApp </iTunes> </Document>";

$Document = new SimpleXMLElement($xmlSource);

foreach ($Document->iTunes as $iTunes)
{
    echo $iTunes, PHP_EOL;
}
查看更多
做个烂人
6楼-- · 2019-01-20 05:10

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; 
}
查看更多
登录 后发表回答