How to use Dom ?- urgent
843834May 2 2003 — edited May 7 2003hi, everyone.
im trying to use DOM and SAX to parse my XML document but i am unsure abt using this XML parsers. Im using Tomcat4.1.24 and jdk1.4. I read up the resources in the net but i still don understand. Can anybody help me???
Coz when i try to save my XML document, it gives and error. NullPointer Exception. Its not dat i didnt add anything but i dono how to use Dom./ Same goes to my login page. It canot detect my userTable.xml so anybosy can logins in..
Firstly, now, im trying to do my login page so this is my code for processing.jsp:
//Page Directives
<%@ page language="java" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.Document" %>
<%@ page import="org.w3c.dom.Element" %>
<%@ page import="org.w3c.dom.DOMException" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.IOException" %>
//Page Variables
<%! String fileRoot = "e:/www/testpro/"; %>
<%! String root = "http://fiza/testpro/"; %>
<%! String dataRoot = "http://fiza/testpro/data/"; %>
<%! String fileName= dataRoot + "userTable.xml"; %>
<jsp:useBean id = "logon" scope = "session" class = "logonBean.logon"/>
<%! String msg="Parse Successful!";%>
<%! String nodeValue="No Value";%>
<%! String userID="";%>
<%! String pwd = "";%>
<%! String routeURL="";%>
<%! int listLength;%>
<html>
<head>
<title>processlogon.jsp</title>
</head>
<body>
<%
//Get page parameters:
if(request.getParameter("userID") != null)
{
userID = request.getParameter("userID");
}
if(request.getParameter("pwd") != null)
{
pwd = request.getParameter("pwd");
}
//Declare variables
Document document;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//Initialise variables:
msg="Parse Successful! ";
routeURL="logon.jsp";
//Read XML file
try {
//Open the file for reading:
URL u = new URL(fileName);
InputStream inputXML = u.openStream();
//Build document:
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(inputXML);
//Generate the NodeList;
org.w3c.dom.NodeList nodeList = document.getElementsByTagName("user");
listLength = nodeList.getLength();
//Search for User's Record
outer:
for (int i=0; i<nodeList.getLength(); i++)
{
org.w3c.dom.Node curNode = nodeList.item(i);
//Get userID attribute:
Element curElm = (Element)nodeList.item(i);
String curUserID = curElm.getAttribute("userID");
//Get pwd attribute:
String curPwd = curElm.getAttribute("pwd");
if (curUserID.equals(userID) && curPwd.equals(pwd))
{
routeURL = "ViewProj_list.jsp";
logon.setUserID(userID);
logon.setSecure();
break outer;
} //end if
}//end for
//Exception Handling
}catch(Exception e)
{
msg = msg + e.toString();
}
%>
<%=msg%>
//Page Routing
<script language="javascript">setTimeout("document.location='<%=routeURL%>'",100)</script>
</body>
</html>
This is my userTable.xml:
<?xml version="1.0" encoding="UTF-8"?>
<userTable>
<user userID = "ADMIN" pwd = "Admin">ADMIN</user>
<user userID = "STAFF" pwd = "Staff">STAFF</user>
</userTable>
As u can see from there, i actually want the admin to go to ViewProj_list.jsp and staff to go to ViewStaff_list.jsp.. But im not so sure how..
Please help me...Its urgent..
Thanx..