使用 PHP 的 simpleXML 解析 XML

发布于 2024-08-23 17:50:20 字数 701 浏览 10 评论 0原文

我正在学习如何使用 PHP 的简单 XML 来解析 XML。我的代码是:

<?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);
?>

当它运行时,它返回一个空白屏幕,没有错误。如果我从文档标记中删除所有属性,它将返回:

myApp SimpleXMLElement Object ( [0] => myApp )

这是预期的结果。

我做错了什么?请注意,我无法控制 XML 源,因为它来自 Apple。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

樱娆 2024-08-30 17:50:20

您的 xml 包含默认名称空间。为了使您的 xpath 查询正常工作,您需要注册此名称空间,并在您正在查询的每个 xpath 元素上使用名称空间前缀(只要这些元素都属于同一名称空间,就像您的示例中所做的那样):

$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; 
}

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; 
}
铜锣湾横着走 2024-08-30 17:50:20

有关默认命名空间的部分,请阅读 fireeyedboy 的回答。如前所述,如果要在默认命名空间中的节点上使用 XPath,则需要注册命名空间。

但是,如果您不使用 xpath(),SimpleXML 有它自己的魔力,可以自动选择默认名称空间。

$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;
}

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;
}
っ左 2024-08-30 17:50:20

这是一般示例

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";
}

,有关更多信息,请检查:
http:// /www.yasha.co/XML/how-to-parse-xml-with-php-simplexml-DOM-Xpath/article-1.html

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

怂人 2024-08-30 17:50:20

此行:

print_r($result);

位于 foreach 循环之外。也许你应该尝试

print_r($results);

一下。

This line:

print_r($result);

is outside the foreach loop. Maybe you should try

print_r($results);

instead.

巡山小妖精 2024-08-30 17:50:20

看来如果你在 xpath 上使用通配符(//)它就会起作用。另外,不知道为什么,但如果您从 Document 元素中删除名称空间属性 (xmlns),则当前的代码将起作用。也许是因为没有定义前缀?无论如何,以下应该有效:

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

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;  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文