English 中文(简体)
用简单的xml
原标题:Find if node has siblings using simplexml

我试图找到某位兄弟是否拥有兄弟,如果是的话,我想知道这些兄弟是什么。

这是可能的吗?

最佳回答

为了选择兄弟姐妹,你必须使用相应的XPath轴。 这里是如何选择所有兄弟姐妹(调整 no子本身)

$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()."
";    
?>

成果:

title
from
body
$xml = new SimpleXMLElement($xmlstr);
$xmlNode = $xml->xpath( root/yourNodeName );
$nodeCount = count($xmlNode); 

不知道这是否仍然对你有用





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签