日期:2014-05-17 浏览次数:20733 次
$xml=simplexml_load_file('bb.xml'); foreach($xml->item as $v){ if($v->id==4) echo "id : $v->id<br>pd : $v->pd"; }
------解决方案--------------------
xpath,还有其他答案吗。
[User:root Time:10:28:18 Path:/home/liangdong/php]$ php xpath.php pd B004 [User:root Time:10:28:19 Path:/home/liangdong/php]$ cat xpath.php <?php $str = <<<EOF <channel> <item> <id>1</id> <pd>B007</pd> </item> <item> <id>2</id> <pd>B001</pd> </item> <item> <id>3</id> <pd>B000</pd> </item> <item> <id>4</id> <pd>B004</pd> </item> <item> <id>5</id> <pd>B003</pd> </item> </channel> EOF; $xml = simplexml_load_string($str, "SimpleXMLElement", LIBXML_NOBLANKS); $res = $xml->xpath("/channel/item/id[text()=4]/parent::item/pd"); foreach ($res as $node) { echo $node->getName() . " " . $node->{0} . PHP_EOL; } ?>