books.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<library>
<book>
<name>Head First Java, 2nd Edition</name>
<author>Kathy Sierra and Bert Bates</author>
<publication-date>09-Feb-2005</publication-date>
</book>
<book>
<name>Effective Java</name>
<author>Joshua Bloch</author>
<publication-date>28-May-2008</publication-date>
</book>
<book>
<name>Java How to Program, 7th Edition</name>
<author>Harvey M. Deitel and Paul J. Deitel</author>
<publication-date>6-Jan-2007</publication-date>
</book>
</library>
------------------------------------------------------------
readDOMXML.jsp
<%@ page language="java" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="javax.xml.parsers.DocumentBuilder" %>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory" %>
<%
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db =dbf.newDocumentBuilder();
Document doc=db.parse("c:\\books.xml");
NodeList nl = doc.getElementsByTagName("book");
%>
<html>
<head>
<title>How to read XML file in JAVA</title>
</head>
<body>
<%
for(int i=0;i<nl.getLength();i++)
{
NodeList nameNlc= doc.getElementsByTagName("name");
Element nameElements=(Element)nameNlc.item(i);
String nameTagValue=nameElements.getChildNodes().item(0).getNodeValue();
NodeList authorNlc= doc.getElementsByTagName("author");
Element authorElements=(Element)authorNlc.item(i);
String authorTagValue=authorElements.getChildNodes().item(0).getNodeValue();
NodeList dateNlc= doc.getElementsByTagName("publication-date");
Element dateElements=(Element)dateNlc.item(i);
String dateTagValue=dateElements.getChildNodes().item(0).getNodeValue();
out.println("name :"+nameTagValue+"<br>");
out.println("author :"+authorTagValue+"<br>");
out.println("publication-date :"+dateTagValue+"<br><br>");
}
%>
</body>
</html>
0 comments:
Post a Comment