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