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

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