帮助!通过 php simplexml 获取节点值!
我只想从 xml 节点获取值。所以我遵循 php 文档中的代码: SimpleXMLElement::xpath() 。但是没有。而且我觉得Xpath更不方便,有没有更好的方法来获取我想要的节点??!
我的php代码:
<?php
/**
* @author kevien
* @copyright 2010
*/
$arr = array ();
$xml = simplexml_load_file("users.xml");
$result = $xml->xpath('/users/user[@id="126"]/watchHistory/whMonthRecords[@month="2010-09"]/whDateList/date');
while(list( , $node) = each($result)) {
array_push($arr, $node);
}
print_r($arr);
?>
它返回:
Array ( [0] => SimpleXMLElement Object ( [0] => 02 ) [1] => SimpleXMLElement Object ( [0] => 03 ) [2] => SimpleXMLElement Object ( [0] => 06 ) [3] => SimpleXMLElement Object ( [0] => 10 ) [4] => SimpleXMLElement Object ( [0] => 21 ) )
我的users.xml部分:
<users>
<user id="126">
<name>老黄牛三</name>
<watchHistory>
<whMonthRecords month="2010-09">
<whDateList month="2010-09">
<date>02</date>
<date>03</date>
<date>06</date>
<date>10</date>
<date>21</date>
</whDateList>
</<whMonthRecords>
</<watchHistory>>
</user>
</users>
非常感谢!
I just want to get the value from xml node.So I following the code from php document:
SimpleXMLElement::xpath() .But it didn't.And I thought the Xpath is much more inconvenience ,is there a much better way to get the node I want??!
my php code:
<?php
/**
* @author kevien
* @copyright 2010
*/
$arr = array ();
$xml = simplexml_load_file("users.xml");
$result = $xml->xpath('/users/user[@id="126"]/watchHistory/whMonthRecords[@month="2010-09"]/whDateList/date');
while(list( , $node) = each($result)) {
array_push($arr, $node);
}
print_r($arr);
?>
it returns:
Array ( [0] => SimpleXMLElement Object ( [0] => 02 ) [1] => SimpleXMLElement Object ( [0] => 03 ) [2] => SimpleXMLElement Object ( [0] => 06 ) [3] => SimpleXMLElement Object ( [0] => 10 ) [4] => SimpleXMLElement Object ( [0] => 21 ) )
my part of users.xml :
<users>
<user id="126">
<name>老黄牛三</name>
<watchHistory>
<whMonthRecords month="2010-09">
<whDateList month="2010-09">
<date>02</date>
<date>03</date>
<date>06</date>
<date>10</date>
<date>21</date>
</whDateList>
</<whMonthRecords>
</<watchHistory>>
</user>
</users>
Thank you very much!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将整个循环替换为:
甚至:
Replace your whole loop with:
or even: