这应该是一件容易的事,但我只是没有让它工作:在下面的代码片段中,我想选择具有值为“Categoryname2”的子节点<WebFilterCategory>的节点:<Name>
<?php
$xmlstring = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<Login>
<UserName>admin</UserName>
<Password>admin</Password>
</Login>
<Set Operation="get">
<WebFilterCategory transactionid="">
<Name>Categoryname1</Name>
<Classification>Objectionable</Classification>
<DomainList>
<Domain>example1.com</Domain>
<Domain>example2.com</Domain>
</DomainList>
</WebFilterCategory>
<WebFilterCategory transactionid="">
<Name>Categoryname2</Name>
<Classification>Objectionable</Classification>
<DomainList>
<Domain>example1.org</Domain>
<Domain>example2.org</Domain>
</DomainList>
</WebFilterCategory>
</Set>
</Request>
XML;
$xml = simplexml_load_string( $xmlstring ) or die("Error: Cannot create object");
foreach ($xml->query('//WebFilterCategory/Name[contains(., "Categoryname2")]') as $category) {
print_r($xmlstring);
}
?>
可能还有一个更精简的查询来获得所需的结果。
HUX布斯