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

在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 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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?