首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >simplexml获取匹配节点旁边的节点的值

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

Stack Overflow用户
提问于 2013-05-02 07:30:51
回答 1查看 191关注 0票数 0

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

代码语言:javascript
复制
<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> 

这是我用来获取描述内容的查询,但我也想要与描述匹配的链接。

代码语言:javascript
复制
    $userIN = "cell";
    $nod2 = $xml->xpath 
  ("//description
              [text( 
                [contains
                 (translate(.,'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz'),
                   '".$userIN."')]]");
EN

回答 1

Stack Overflow用户

发布于 2013-05-02 07:33:35

我借用了另一个主题的答案:

SimpleXML get next/prev node

希望这能有所帮助。

代码语言:javascript
复制
$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++;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16328068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档