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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Retrieving Data From the XML file 
 

In this example we will examine how to parse and expose XML information using the JAXP with a JSP page. This example is only geared towards showing how to construct a Java object from an XML document.

 

Retrieving Data From the XML file

                         

In this example we will examine how to parse and expose XML information using the JAXP with a JSP page. This example is only geared towards showing how to construct a Java object from an XML document. 

By this example we are going to get the XML data from the xml file in our jsp file. We have a XML file in which we have some data. What we need is to retrieve the data from the xml file. This xml file has all the information about the airline schedule. 

To make a program over it we have used the following page directive attributes, the first attribute we have used is contentType and the second attribute is import. We need to import some classes and packages which we have described in the program. 

Inside the scriptlet firstly we will define a factory API which allows our application to obtain a Java XML  parser. DocumentBuilderFactory defines a factory API. Now create a DocumentBuilder object to parse an org.w3c.dom.Document from XML. Save the xml file at bin file of  C:\apache-tomat-5.5.23\bin\. Now we need to parse method to actually parse the XML file to create our own Document object. Document object will be created by parse method of the DocumentBuilder

From the Document object we will get a NodeList object represents all of the elements in our XML document name message. NodeList object will be obtained by the getElementByTagName(). 

A NodeList is an array starting from 0 and going up to the length of the array by calling getFirstChild() method. By using this method the desired text node is returned and we can use getNodeValue() for our message. 

The code of the airLineShedule.xml file:

<?xml version="1.0" encoding="UTF-8"?>
 <Flights>
   <AirCarrier>AA</AirCarrier>
   <FlightNumber>700</FlightNumber>
  
      <City>DFW</City>
      <Date>30Mar</Date>
      <Scheduled>9.0am
      </Scheduled>
      <Status>ON TIME</Status>
      <Gate>C16</Gate>
      <OffGateTime>0644A</OffGateTime>
      <OffGroundTime>0632A</OffGroundTime>
   
</Flights>

Code of the airLineSheduler.jsp file:

<!-- page Directive used -->
<%@ page contentType="text/html"%>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory,
javax.xml.parsers.DocumentBuilder,org.w3c.dom.*"
%>
<html>
<body><center><table border="2">

<%
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("\\airLineShedule.XML");

NodeList nl= doc.getElementsByTagName("AirCarrier");
NodeList n1= doc.getElementsByTagName("FlightNumber");
NodeList n2= doc.getElementsByTagName("City");
NodeList n3= doc.getElementsByTagName("Date");
NodeList n4= doc.getElementsByTagName("Scheduled");
NodeList n5= doc.getElementsByTagName("Status");
NodeList n6= doc.getElementsByTagName("Gate");
NodeList n7= doc.getElementsByTagName("OffGateTime");
NodeList n8= doc.getElementsByTagName("OffGroundTime");

%>

<tr>
<td colspan="2"><h4>AirLine Shedule</h4></td> 
</tr>
<tr>
<td>AirCarrier</td>
<td><%= nl.item(0).getFirstChild().getNodeValue() %> </td>
</tr>
<tr><td>FlightNumber</td>
<td><%= n1.item(0).getFirstChild().getNodeValue() %> </td>
</tr>
<tr>
<td>City</td>
<td><%= n2.item(0).getFirstChild().getNodeValue() %> </td></tr>
<tr><td>Date</td>
<td><%= n3.item(0).getFirstChild().getNodeValue() %> </td></tr>
<tr><td>Scheduled</td>
<td><%= n4.item(0).getFirstChild().getNodeValue() %> </td></tr>
<tr><td>Status</td>
<td><%= n5.item(0).getFirstChild().getNodeValue() %> </td></tr>
<tr><td>Gate</td>
<td><%= n6.item(0).getFirstChild().getNodeValue() %> </td></tr>
<tr><td>OffGateTime</td>
<td><%= n7.item(0).getFirstChild().getNodeValue() %> </td>
</tr>
<tr><td>OffGroundTime</td>
<td><%= n8.item(0).getFirstChild().getNodeValue() %> 
</td>
</tr>
</table>
</center>
</body>
</html>

 

The output of the program is given below:

 

Download this example.

Download airLineShedule.xml file.

Download airLineSheduler.txt file.

                         

» View all related tutorials
Related Tags: java c string jsp ide class strings script object io objects method get char ip using sequence id declaration length

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:

It was really wat i wanted, but i don't know how to run it. It should have been clear enough for a novice like me. Hope ur tutorials will be useful for all kinds of people in future.
Thanks,
Pradeep

Posted by venkat on Monday, 10.15.07 @ 18:37pm | #33862

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.