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!
The tagName read-only property of the Element interface returns the tag name of the element on which it's called. For example, if the element is an <img> , its tagName property is "IMG" (for HTML documents; it may be cased differently for XML/XHTML documents).
The DOMElement::getElementsByTagName() function is an inbuilt function in PHP which is used to get the elements by tagname. Parameters: This function accepts a single parameter $name which holds the tag name or use * for getting all the tags.
The DOMDocument::getElementsByTagName() function is an inbuilt function in PHP which is used to return a new instance of class DOMNodeList which contains all the elements of local tag name.
A tag name is a part of a DOM structure where every element on a page is been defined via tag like input tag, button tag or anchor tag etc. Each tag has multiple attributes like ID, name, value class etc. As far as other locators in Selenium are concerned, we used these attributes values of the tag to locate elements.
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.
$nodelist = $results->getElementsByTagName('*');
for( $i=0; $i < $nodelist->length; $i++)
echo $nodelist->item($i)->nodeName;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With