VB icon

Reading XML File Using JSP

Email
Submitted on: 9/15/2015 11:56:30 AM
By: Jake R. Pomperada  
Level: Beginner
User Rating: Unrated
Compatibility: Java (JDK 1.1), Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 2882
author picture
 
     In this article I would like to show you how to read XML or Extensible Markup Language using JSP or Java Server Pages. The code is quiet complex but it is not difficult as you may think. JSP is the language used in creating enterprise Java applications for the web. If you have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Reading XML File Using JSP
// Description:In this article I would like to show you how to read XML or Extensible Markup Language using JSP or Java Server Pages. The code is quiet complex but it is not difficult as you may think. JSP is the language used in creating enterprise Java applications for the web.
If you have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.
// By: Jake R. Pomperada
//**************************************

name.xml
<people>
 <person>
<name>Joe Zamora</name>
<age>30</age>
 </person>
 <person>
<name>Rob Dela Rosa</name>
<age>29</age>
 </person>
 <person>
<name>Ana Tan</name>
<age>45</age>
 </person>
 <person>
<name>Raul Smith</name>
<age>61</age>
 </person>
<person>
<name>Leslie Paul Adams</name>
<age>53</age>
 </person>
</people>
read.jsp
<%@page import="org.w3c.dom.*, javax.xml.parsers.*" %>
<%
 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
 Document doc = docBuilder.parse("http://localhost:8080/xml_reader/name.xml");
%>
<%!
 public boolean isTextNode(Node n){
 return n.getNodeName().equals("#text");
 }
%>
<html>
<title> Reading Users Name From XML File </title>
<head>
</head>
<style>
label {
 display: block;
 float: left;
 width : 250px;
 font-size:20px;
}
input, select {
width: 200px;
border: 2px solid #000;
padding: 0;
margin: 0;
height: 30px;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
input {
text-indent: 4px;
}
</style>
<!-- Code for translating thai language in the web browser -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <body>
 <div class="container">
 <h3>Reading Users Name From XML File</h3>
</div>
<br><br>
<form method="post" action="read.jsp">
<label> FIRST SELECTION </label>
<select id="select" class="select" name="filter" style="text-align:center;">
 <option value="1" ${param.filter == '1' ? 'selected' : ''}> 1st Choice</option>
 <option value="2" ${param.filter == '2' ? 'selected' : ''}> 2nd Choice</option>
 <option value="3" ${param.filter == '3' ? 'selected' : ''}> 3rd Choice</option>
 <option value="4" ${param.filter == '4' ? 'selected' : ''}> 4nd Choice</option>
</select> <br><br>
 <input type="submit" id="submit" name="submit" value="OK" title="Click here to select your choice.">
</form> <br>
 
 <!--Document doc = builder.parse("http://localhost:8080/xml_reader/users_xml/users.xml"); -->
<%
String option = request.getParameter("filter"); 
 
if("1".equals(option)){
%> 
 <table border='2'>
<tr>
<th>NAME</th>
<th>AGE</th>
</tr> 
 <%Element element = doc.getDocumentElement(); 
 NodeList personNodes = element.getChildNodes(); 
 for (int i=0; i<personNodes.getLength(); i++){
Node emp = personNodes.item(i);
if (isTextNode(emp))
continue;
NodeList NameDOBCity = emp.getChildNodes(); 
 %>
 <tr>
 <%
 for (int j=0; j<NameDOBCity.getLength(); j++ ){
Node node = NameDOBCity.item(j);
if ( isTextNode(node)) 
continue;
%>
<td> <%= node.getFirstChild().getNodeValue() %></td>
<%
 } 
%>
</tr>
<%
 }
%>
 </table>
 <% } %>
 
 </body>
</html>


Other 53 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.