下面我有一个 PHP 脚本,我需要搜索一个 XML 文件并找到<AnotherChild>
. 出于某种原因,目前它返回 0 结果,我不知道为什么。如果有人能明白为什么它返回 0 结果,如果他们能让我知道原因,我将不胜感激。
XML:
<TransXChange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.transxchange.org.uk/" xsi:schemaLocation="http://www.transxchange.org.uk/ http://www.transxchange.org.uk/schema/2.1/TransXChange_general.xsd" CreationDateTime="2013-07-12T18:12:21.8122032+01:00" ModificationDateTime="2013-07-12T18:12:21.8122032+01:00" Modification="new" RevisionNumber="3" FileName="swe_44-611A-1-y10.xml" SchemaVersion="2.1">
<Node1>...</Node1>
<Node2>...</Node2>
<Node3>...</Node3>
<Node4>...</Node4>
<Node5>...</Node5>
<Node6>...</Node6>
<Node7>
<Child>
<id>ABCDEFG123</id>
</Child>
<AnotherChild>
<id>ABCDEFG124</id>
</AnotherChild>
</Node7>
<Node8>...</Node8>
</TransXChange>
PHP:
<?php
$xmldoc = new DOMDocument();
$xmldoc->load("directory1/directory2/file.xml");
$xpathvar = new DOMXPath($xmldoc);
$xpathvar->registerNamespace('transXchange', 'http://www.transxchange.org.uk/');
$queryResult = $xpathvar->query('//AnotherChild/id');
foreach($queryResult as $result) {
echo $result->textContent;
}
?>
谢谢