【发布时间】:2017-09-04 13:32:44
【问题描述】:
我在 XML 中有如下元素
<user:name test:one = "firstUser" />
我使用 PHP DOM Xpath 来读取 XML
$doc = new DOMDocument();
$xpath = new DOMXPath($doc);
$xml = simplexml_load_file(file path here);
echo '<pre>'; print_r($xml);
输出为空
SimpleXMLElement Object
(
)
如果我尝试删除 : 和如下前缀
<name one = "firstUser" />
然后它读取元素。输出是
SimpleXMLElement Object
(
[name] => SimpleXMLElement Object
(
[@attributes] => Array
(
[one] => firstUser
)
)
)
如何使用 前缀和冒号 (:)
读取元素值更新:示例 XML 文件
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:test="http://www.w3.org/2001/XMLSchema" xmlns:user="http://www.w3.org/2001/XMLSchema">
<user:name test:one = "firstUser" />
<name second = "secondUser" />
</root>
【问题讨论】:
-
在php.net/manual/en/function.simplexml-load-file.php 中添加
user作为命名空间,也可以添加test -
还是同样的问题
-
你用的是什么php版本?检查这个:3v4l.org/ijeHp 它可以工作,但有一些警告
-
PHP 版本 5.6.19
-
它有效,但如果我使用外部 XML 文件同样的问题