dtnpzghys01643322
2018-01-30 11:59
浏览 86
已采纳

在PHP SimpleXML中获取节点名称

I've alread saw this question and answer but I wasn't able to use it.

This is my code:

//Get grandparents of MyRefNode
$GrandParents = $MyXml->xpath('//MyRefNode/../..');
//Loop grandparents 
foreach ($GrandParents as $GrandParent){
    //Count each GrandParent's children
    $count = $GrandParent->count(); 
    echo '<br/> Children count = '.$count;
    //Get GrandParent's Tag name **NOT WORKING**
    $TName = $GrandParent->xpath('./name()')
    echo '<br/> Tag Name = '.$TName;
}

图片转代码服务由CSDN问答提供 功能建议

我已经看过这个问题和答案但我无法使用它。

这是我的代码:

  //获取MyRefNode的祖父母
 $ GrandParents = $ MyXml-&gt; xpath('// MyRefNode /../ ..');  
 //循环祖父母
foreach($ GrandParents as $ GrandParent){
 //计算每个GrandParent的子项
 $ count = $ GrandParent-&gt; count();  
 echo'&lt; br /&gt; 儿童数='。$ count; 
 //获取GrandParent的标签名称**不工作** 
 $ TName = $ GrandParent-&gt; xpath('。/ name()')
 echo'&lt; br /  &GT; 标签名称='。$ TName; 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyi1963 2018-01-30 12:17
    已采纳

    You should be able to use SimpleXMLElement::getName() instead :

    foreach ($GrandParents as $GrandParent){
        ....
        $TName = $GrandParent->getName();
        echo '<br/> Tag Name = '.$TName;
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题