Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Accessing XML file from Java 
 

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>
[an error occurred while processing this directive]

Here is the code of AccessingXmlFile.javah

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:

                         

» View all related tutorials
Related Tags: c xml api file dom console directory help get display vi name using ole this id ai element check elements

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

System.out.println("Last Name :"+((Node)lastName.item(0).getNodeValue());

in the code is wrong

It will not work in SOLARIS Env.

Posted by kiran on Tuesday, 10.14.08 @ 13:03pm | #81078

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.