dtnpzghys01643322
dtnpzghys01643322
2018-01-30 11:59

在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;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douyi1963 douyi1963 3年前

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

    foreach ($GrandParents as $GrandParent){
        ....
        $TName = $GrandParent->getName();
        echo '<br/> Tag Name = '.$TName;
    }
    
    点赞 评论 复制链接分享

为你推荐