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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Parsing The XML File Using JDOM Parser in JSP 
 

In this example we show how to work with JDOM parser to parse the xml document. JDOM can read existing XML documents from files, network sockets, strings, or from reader.

 

Parsing The XML File Using JDOM Parser in JSP

                         

In this example we show how to work with JDOM parser to parse the xml document. JDOM can read existing XML documents from files, network sockets, strings, or from reader. JDOM does not has its own native parser. XML parsing produces an xml parse tree from an XML document. Using the JDOM packages, it's very simple to work with parsed XML documents.

Working with XML document, use the following from JDOM package:
1. Simply make an object of org.jdom.input.SAXBuilder with no-args constructor
2. Call the Builder's build() method to build a document object from a reader, InputStream, URL, File or a String containing a system ID.
3. Navigate the document using the methods of the document class, the Element class, and the other JDOM classes. 

For Example:                  

SAXBuilder saxBuilder = new SAXBuilder();

Document doc = saxBuilder.build("http://localhost:8080/
ServletExample/roseindia.xml
");

The following example file 'JDOMparsingxml.jsp' will show you the use of JDOM parser to parse the 'roseindia.xml' file and display it to the page.

JDOMparsingxml.jsp

<%page import"org.jdom.*, java.util.*, 
org.jdom.input.SAXBuilder" 
%>
<%
  SAXBuilder saxBuilder = new SAXBuilder();
  Document doc = saxBuilder.build
  (
"http://localhost:8080/ServletExample/roseindia.xml");
%>
<html>
  <head><title>Parsing of xml using JDOM</title></head>
  <body>
    <h1><font color='green'>Employees of Roseindia</font></h1>
    <table border="2">
      <tr>
        <th><font color='blue'>Name of Employee</font></th>
        <th><font color='blue'>Date of Birth</font></th>
        <th><font color='blue'>City</font></th>
      </tr>
      <%
      List list = doc.getRootElement().getChildren();
      Iterator iter = list.iterator();
      while (iter.hasNext()){
        Element element = (Elementiter.next();
        List NameDOBCity  = element.getChildren();
        Iterator listIter = NameDOBCity.iterator();
      %>
      <tr>
      <%
        while listIter.hasNext() ){
          Element childNode = (ElementlistIter.next();
      %>       
        <td><%= childNode.getText() %></td>
      <%
        }
      }
      %>
      </tr>
    </table>
  </body>
</html>

roseindia.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<roseindia>

  <employee>
    <name>Sandeep Kumar suman</name>
    <date-of-birth>15-02-84</date-of-birth>
  <city>Gorakhpur</city>
  </employee>

  <employee>
    <name>Amit Raghuvanshi</name>
    <date-of-birth>19-12-86</date-of-birth>
  <city>Lucknow</city>
  </employee>

  <employee>
    <name>Vineet Bansal</name>
    <date-of-birth>05-02-83</date-of-birth>
  <city>Sonipat</city>
  </employee>

  <employee>
    <name>Vikas Singh</name>
    <date-of-birth>05-12-88</date-of-birth>
  <city>Hazipur</city>
  </employee>

  <employee>
    <name>Anusmita Singh</name>
    <date-of-birth>28-12-84</date-of-birth>
  <city>Bareilly</city>
  </employee>

</roseindia>

Run the program on this url: http://localhost:8080/ServletExample/jsp/JDOMparsingxml.jsp  and following output will be displayed

Download Source Code

                         

» View all related tutorials
Related Tags: java html c web jsp dynamic static script servlet sed ip page tag this log tar close js start technology

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 
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.