genespos
genespos

Reputation: 3311

Get node name in 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;
}

Upvotes: 1

Views: 1687

Answers (1)

har07
har07

Reputation: 89325

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

foreach ($GrandParents as $GrandParent){
    ....
    $TName = $GrandParent->getName();
    echo '<br/> Tag Name = '.$TName;
}

Upvotes: 3

Related Questions