Search code examples
phpxmldomgetelementsbytagname

get tagname in xml using php


Here is my xml:

<news_item>    
    <title>TITLE</title>
    <content>COTENT.</content>
    <date>DATE</date>
<news_item>

I want to get the names of the tags inside of news_item.

Here is what I have so far:

$dom = new DOMDocument();
$dom->load($file_name);
$results = $dom->getElementsByTagName('news_item');

WITHOUT USING other php libraries like simpleXML, can I get the name of all the tag names (not values) of the children tags?

Example solution

title, content, date

I don't know the name of the tags inside of news_item, only the container tag name 'news_item'

Thanks guys!


Solution

  • Try this:

    foreach($results as $node)
    {
        if($node->childNodes->length)
        {
            foreach($node->childNodes as $child)
            {
                echo $child->nodeName,', ';
            }
        }
    }
    

    Should work. Using something similar currently, though for html not xml.