Share on Google+Share on Google+

Example of getTagName() method in xml dom api.

In this section, you will see the example of getTagName() method of DOM api.

Example of getTagName() method.

In This Example, we implement the method getTagName() and getDocumentElement().

Here we see in this code given below that getTagName() method returns the name of this Element not the value of the name attribute. The getDocumentElement() gives an XmlElement class represented the element.

In this example, we create the Dom parser by create the instance of DocumentBuilder  using DocumentBuilderFactory .

After parsing the xml file we find the root node and get the tag name as output.

The code of the program is as follows.


<?xml version="1.0" encoding="UTF-8"?>
       <item >

import org.w3c.dom.*;
import javax.xml.parsers.*; 
import org.w3c.dom.Document;

class ExamplegetTagName 
  public static void main(String[] args)  throws IOException
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    Document doc = docBuilder.parse("order.xml");
    Element root=doc.getDocumentElement();
    String nl = root.getTagName();     
    System.out.println("the output of getDocumentElement="+root);
  System.out.println("the output of getTagName="+nl);
      }catch(Exception e){}



C:\>java ExamplegetTagName
the output of getDocumentElement = [orders: null]
the output of getTagName = order

Download The Example


Posted on: September 23, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null