SimpleXML:根据子元素的内容选择父节点
SimpleXML: Selecting parent node based on content of child element
这应该是个简单的任务,但我只是没有让它工作:
在下面的代码片段中,我想 select <WebFilterCategory>
节点有一个 <Name>
子节点,其值为 "Categoryname2":
<?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);
}
?>
可能还有更精简的查询来获得所需的结果。
您要查找的方法不是query
而是xpath
然后您可以更新路径以匹配名称包含 Categoryname2
的 WebFilterCategory
//WebFilterCategory[Name[contains(., "Categoryname2")]]'
更新代码
$xml = simplexml_load_string( $xmlstring ) or die("Error: Cannot create object");
foreach ($xml->xpath('//WebFilterCategory[Name[contains(., "Categoryname2")]]') as $category) {
print_r($category);
}
输出
SimpleXMLElement Object
(
[@attributes] => Array
(
[transactionid] =>
)
[Name] => Categoryname2
[Classification] => Objectionable
[DomainList] => SimpleXMLElement Object
(
[Domain] => Array
(
[0] => example3.org
[1] => example2.org
)
)
)
看到一个Php demo
这应该是个简单的任务,但我只是没有让它工作:
在下面的代码片段中,我想 select <WebFilterCategory>
节点有一个 <Name>
子节点,其值为 "Categoryname2":
<?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);
}
?>
可能还有更精简的查询来获得所需的结果。
您要查找的方法不是query
而是xpath
然后您可以更新路径以匹配名称包含 Categoryname2
//WebFilterCategory[Name[contains(., "Categoryname2")]]'
更新代码
$xml = simplexml_load_string( $xmlstring ) or die("Error: Cannot create object");
foreach ($xml->xpath('//WebFilterCategory[Name[contains(., "Categoryname2")]]') as $category) {
print_r($category);
}
输出
SimpleXMLElement Object
(
[@attributes] => Array
(
[transactionid] =>
)
[Name] => Categoryname2
[Classification] => Objectionable
[DomainList] => SimpleXMLElement Object
(
[Domain] => Array
(
[0] => example3.org
[1] => example2.org
)
)
)
看到一个Php demo