使用 PHP 的 simpleXML 解析 XML
我正在学习如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的 xml 包含默认名称空间。为了使您的 xpath 查询正常工作,您需要注册此名称空间,并在您正在查询的每个 xpath 元素上使用名称空间前缀(只要这些元素都属于同一名称空间,就像您的示例中所做的那样):
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):
有关默认命名空间的部分,请阅读 fireeyedboy 的回答。如前所述,如果要在默认命名空间中的节点上使用 XPath,则需要注册命名空间。
但是,如果您不使用
xpath()
,SimpleXML 有它自己的魔力,可以自动选择默认名称空间。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.这是一般示例
,有关更多信息,请检查:
http:// /www.yasha.co/XML/how-to-parse-xml-with-php-simplexml-DOM-Xpath/article-1.html
this is general example
for more information check this :
http://www.yasha.co/XML/how-to-parse-xml-with-php-simplexml-DOM-Xpath/article-1.html
此行:
位于 foreach 循环之外。也许你应该尝试
一下。
This line:
is outside the foreach loop. Maybe you should try
instead.
看来如果你在 xpath 上使用通配符(//)它就会起作用。另外,不知道为什么,但如果您从 Document 元素中删除名称空间属性 (xmlns),则当前的代码将起作用。也许是因为没有定义前缀?无论如何,以下应该有效:
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: