特定のノードに兄弟があるかどうかを調べようとしています。ある場合は、それらの兄弟が何であるかを知りたいです。
これは可能ですか?
ノードの兄弟を選択するには、対応するXPath軸を使用する必要があります。すべてのノードの兄弟を選択する方法は次のとおりです(ノード自体は無視します)
$siblings = $node->xpath('preceding-sibling::* | following-sibling::*');
それがあなたがしなければならないすべてです。
ここでは、xpathを使用することが最善の策だと思います。
<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
<title>Forty What?</title>
<from>Joe</from>
<to>Jane</to>
<body>
I know that's the answer -- but what's the question?
</body>
</document>
XML;
function get_all_siblings(SimpleXMLElement $node)
{
return $node->xpath('preceding-sibling::* | following-sibling::*');
}
$xml = simplexml_load_string($string);
foreach (get_all_siblings($xml->to) as $e)
echo $e->getName()."\n";
?>
結果:
title
from
body
$xml = new SimpleXMLElement($xmlstr);
$xmlNode = $xml->xpath('root/yourNodeName');
$nodeCount = count($xmlNode);
これがまだあなたに役立つかどうかわからない