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



Home Xml Dom Accessing XML file from Java

Related Tutorials


 
 

Share on Google+Share on Google+

Accessing XML file from Java

Advertisement
In this example we have provided you a simple java example with the source code that will make it possible to access the XML file through Java.

Accessing XML file from Java

     

This section is going to tell you how to access a XML file using Java Code.

In this example we have provided you a simple java example with the source code that will make it possible to access the XML file through Java. For that we have used DOM parser. DOM parser that loads the XML file into the memory and makes an object model which can be traversed to get the file elements. For this, we have created two different files:
1) MyFile.xml
2) AccessingXmlFile.java

Source Code for accessing XML file through Java

 

 

 

File MyFile.xml

<?xml version="1.0"?>
<student>
<student-name>
<firstname>Anusmita</firstname>
<lastname>Singh</lastname>
</student-name>

<student-address>
<address>Rohini</address>
<city>Delhi</city>
</student-address>
</student>

Here is the code of AccessingXmlFile.java

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

public class AccessingXmlFile {

 public static void main(String argv[]) {

  try {
  File file = new File("C:\\MyFile.xml");
  DocumentBuilderFactory dbf = 
  DocumentBuilderFactory.newInstance();

  DocumentBuilder db = dbf.newDocumentBuilder();
  Document document = db.parse(file);
  document.getDocumentElement().normalize();
  System.out.println("Root element "+
  document.getDocumentElement().getNodeName());

  NodeList node = document.getElementsByTagName("student");
  System.out.println("Information of the students");

  for (int i = 0; i < node.getLength(); i++) {
  Node firstNode = node.item(i);
  
  if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
  
  Element element = (Element) firstNode;
  NodeList firstNameElemntList = 
   element.getElementsByTagName(
"firstname");
  Element firstNameElement = (Element) firstNameElemntList.item(0);
  NodeList firstName = firstNameElement.getChildNodes();
  System.out.println("First Name:"
  ((Node)firstName.item(
0).getNodeValue());
  
  NodeList lastNameElementList = 
   element.getElementsByTagName(
"lastname");
  Element lastNameElement = (Element) 
   lastNameElementList.item(
0);
  NodeList lastName = lastNameElement.getChildNodes();
  System.out.println("Last Name :"+
  ((Node)lastName.item(
0).getNodeValue());

  NodeList addressList = element.getElementsByTagName("address");
  Element addressElement = (Element) addressList.item(0);
  NodeList address = addressElement.getChildNodes();
  System.out.println("Address : "  +
   ((Node) address.item(
0)).getNodeValue());

  NodeList cityList = element.getElementsByTagName("city");
  Element cityElement = (Element) cityList.item(0);
  NodeList city = cityElement.getChildNodes();
  System.out.println("City : "  
   ((Node) city.item(
0)).getNodeValue());
 }
}
  catch (Exception e) {
  e.printStackTrace();
  }
 }
}

In the above example, the method DocumentBuilderFactory.newInstance() enables applications to obtain a parser that produces DOM. The DocumentBuilder provides the DOM document instances from XML document. The Document refers to the HTML or XML document. The getDocumentElement() method provides the XML root Element. The getElementsByTag Name() provides the tag. Then get the value of node by getNodeValue().

Following output will be displayed on the console:

Download Source Code:

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: May 24, 2009

Related Tutorials

Discuss: Accessing XML file from Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:5
Kapil
July 8, 2011
Useful

Hi, this was informative and educative. thanks Kapil
maenolis
October 3, 2011
closing file

You didn't close the file.
pallive
December 26, 2011
JAVA

Well its quite helpful to me will you please tell me how to open a punjabi words XML file in JAVA through netbeans......
Maria
June 20, 2012
error

I run this file in netbeans. Im reading a xml from a website, and I got this error java.lang.NullPointerException at assigment2.Assigment2.main
abhijeet
September 12, 2012
error

there is some error in the code. i am not able to execute it
DMCA.com