Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Xml Dom Getting The XML Root Element

Related Tutorials


 
 

Share on Google+Share on Google+

Getting The XML Root Element

Advertisement
After reading this section, you will be able to retrieve a root element from the XML document.

Getting The XML Root Element

     

After reading this section, you will be able to retrieve a  root element from the XML document.  The JAXP (Java APIs for XML Processing) provides a common interface for creating and using xml files using the standard SAX, DOM and XSLTs. Here you will see the given example to use DOM interface. 

Description of program:

You need a XML document (file). Both Java and the XML file are kept in the same directory. This program takes a XML file as a String at the console . If the given file exists then it parses the document using parse() method . Before parsing the XML document you need a DocumentBuilder object. For creating this first of all you create a DocumentBuilderFactory. After parsing the XML document you get  the node element using getDocumentElement() method. To get the root element use the getNodeName() method.  

Here is the XML File: Employee-Detail.xml

<?xml version = "1.0" ?>
<Employee-Detail>

<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> Amit2@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> Deepak3@yahoo.com </Emp_E-mail>
</Employee>

</Employee-Detail>

Here is the Java File: GetRootNode.java

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;

public class GetRootNode{
  public static void main(String[] args) {
  try{
  BufferedReader bf = new BufferedReader(
  
new InputStreamReader(System.in));
  System.out.print("Enter xml file name: ");
  String str = bf.readLine();
  File file = new File(str);
  if (file.exists()){
  DocumentBuilderFactory fact = 
   DocumentBuilderFactory.newInstance
();
  DocumentBuilder builder = fact.newDocumentBuilder();
  Document doc = builder.parse(str);
  Node node = doc.getDocumentElement();
  String root = node.getNodeName();
  System.out.println("Root Node: " + root);
  }
  else{
  System.out.println("File not found!");
  }
  }
  catch(Exception e){}
  }
}

Download this example.

Output of the program:

C:\vinod\xml>javac GetRootNode.java

C:\vinod\xml>java GetRootNode
Enter xml file name: Employee-Detail.xml
Root Node: Employee-Detail
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: June 2, 2007

Related Tutorials

Discuss: Getting The XML Root Element   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Reedyseth
May 29, 2011
Getting The XML Root Element - DOM

When I do this in a different class the IDE(netbeans) ask me to report a Exception, it require me a SAXException. How come this posible if we are using DOM and should do a DOMException instead. Here is the code: http://paste.ubuntu.com/614317/
hana
June 1, 2012
software engineering

i am u give me these good help,but what i mean is how to retrieve specific column from xml file and display the data in a jlist .
DMCA.com