Get tag 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 tag names inside news_item.

Here is what I still have:

$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 tag names (not values) of tags for children?

Sample solution

title, content, date

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

Thanks guys!

+3
source share
3 answers

Try the following:

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

Must work. Using something similar at the moment, though for html not xml.

+8
source
$nodelist = $results->getElementsByTagName('*');
for( $i=0; $i < $nodelist->length; $i++)
    echo $nodelist->item($i)->nodeName;
+1
source

[ ]

, simplexml_import_dom(), SimpleXMLElement DOMElement. libxml2. DOMElement SimpleXMLElement . , / .

0

All Articles