
example that pars XML file in JSP

<%@page import="org.w3c.dom.*, javax.xml.parsers.*" %>
<%
DocumentBuilderFactory docFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse("C:/roseindia.xml");
%>
<%!
public boolean isTextNode(Node n){
return n.getNodeName().equals("#text");
}
%>
<html>
<head><title>Parsing of xml using DOM Parser</title></head>
<body>
<h2><font color='green'>Employees of Roseindia</font></h2>
<table border="2">
<tr>
<th>Name of Employee</th>
<th>Address</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 nl = emp.getChildNodes();
%>
<tr>
<%
for (int j=0; j<nl.getLength(); j++ ){
Node node = nl.item(j);
if ( isTextNode(node))
continue;
%>
<td><%= node.getFirstChild().getNodeValue() %></td>
<%
}
%>
</tr>
<%
}
%>
</table>
</body>
</html>

Here is the roseindia.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<roseindia>
<employee>
<name>A</name>
<address>Delhi</address>
</employee>
<employee>
<name>B</name>
<address>Delhi</address>
</employee>
<employee>
<name>C</name>
<address>Delhi</address>
</employee>
<employee>
<name>D</name>
<address>Delhi</address>
</employee>
</roseindia>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.