simplexml获取匹配节点旁边的节点的值

时间:2013-05-01 23:30:51

标签: php xml simplexml

我想知道我是否可以获得邻居节点的值

<item>
   <title>Cell Phone Plans That Make You Go Hmmm</title> 
   <link>http://www.articlegeek.com/computers/telecommunication_articles/10574-    cellphoneplanst.htm</link> 
    <description>Cell phone plans across the globe vary a great deal. Some say that cell phone  plans in the US are more costly. However, there are some advantages to the cell phone plans in the US which may balance the difference.</description> 
</item> 

这是我获取描述内容的查询,但我想要的链接也与描述相符。

    $userIN = "cell";
    $nod2 = $xml->xpath 
  ("//description
              [text( 
                [contains
                 (translate(.,'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz'),
                   '".$userIN."')]]");

1 个答案:

答案 0 :(得分:0)

我从另一个主题中借用了这个答案:

SimpleXML get next/prev node

希望这有帮助。

$xmlData = new SimpleXMLElement(file_get_contents("data.xml"));
$index = 0;
foreach($xmlData->row as $item) {
    if ($item->url == $_GET['id']) {
        // show photo
        $title = $item->title;

        $prev = $xmlData->row[$index-1];
        $next = $xmlData->row[$index+1];
    }
    $index++;
}