我正在使用org.xml.sax.SAXParseException; lineNumber:1,在尝试运行以下代码时:
package test;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Random;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.xml.sax.InputSource;
import com.jxml.quick.QDoc;
import com.jxml.quick.Quick;
import test.DDTestPaper1Schema;
public class ValidateTestLogin extends HttpServlet implements Servlet {
//public static String XMLFILEPATH_DD = "D:\\BPS Workspace\\LatestWAR\\JavaSource\\test\\DDTestPaper1.xml";
// public static String XMLFILEPATH_DD = "/testResources/DD/DDTestPaper1.xml";
// public static String XMLFILEPATH_DR = "/testResources/DR/DRTestPaper1.xml";
// public static String XMLFILEPATH_DQ = "/testResources/DQ/DQTestPaper1.xml";
String XMLFILEPATH_DD = "/testResources/DD/DDTestPaper";
String XMLFILEPATH_DR = "/testResources/DR/DRTestPaper";
String XMLFILEPATH_DQ = "/testResources/DQ/DQTestPaper";
String DOTXML = ".xml";
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public ValidateTestLogin() {
super();
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest arg0, HttpServletResponse arg1)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
System.out.println("doGet() method is called ");
doGet(req,res);
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest arg0, HttpServletResponse arg1)
*/
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
System.out.println("doPost() method is called ");
//System.out.println(getServletContext().getRealPath("DDTestPaper1.xml"));
String userName = req.getParameter("txtUserName");
String emailId = req.getParameter("txtEmailAdd");
String selectedTestPaper = req.getParameter("selTestPaper");
HttpSession httpSession = req.getSession(true);
//String selectedTestPaper = httpSession.getAttribute("selectedTestPaper").toString();
boolean validate = validateInput(httpSession, userName, emailId, selectedTestPaper);
if (validate)
{
httpSession.setAttribute("userName",userName);
httpSession.setAttribute("emailId",emailId);
httpSession.setAttribute("selectedTestPaper",selectedTestPaper);
httpSession.removeAttribute("testLoginError");
boolean xmlBindFlag = bindXMLToBean(httpSession, selectedTestPaper);
if (! xmlBindFlag)
{
// reload login page again with error massage
res.sendRedirect("test/testLogin.jsp");
res.flushBuffer();
return;
}
res.sendRedirect("test/testPage.jsp");
res.flushBuffer();
return;
}
else
{
httpSession.removeAttribute("userName");
httpSession.removeAttribute("emailId");
httpSession.removeAttribute("selectedTestPaper");
// reload login page again with error massage
res.sendRedirect("test/testLogin.jsp");
res.flushBuffer();
return;
}
}
private synchronized boolean bindXMLToBean(HttpSession httpSession,String selectedTestPaper )
{
boolean valid = true;
User userObj = null;
Random ran = new Random() ;
int fileNum = ran.nextInt(10)+1;
//if(fileNum < 0)
// {
// fileNum = 4;
// }
System.out.println("FILENUMBEER>>>>>>>>>>>>"+fileNum);
try
{
if (selectedTestPaper.equalsIgnoreCase("DD"))
{
DDTestPaper1Schema userSchema = new DDTestPaper1Schema();
QDoc doc = parseInputXML(userSchema, XMLFILEPATH_DD + fileNum + DOTXML);
userObj = (User) Quick.getRoot(doc);
}
else if (selectedTestPaper.equalsIgnoreCase("DR"))
{
DRTestPaper1Schema userSchema = new DRTestPaper1Schema();
QDoc doc = parseInputXML(userSchema, XMLFILEPATH_DR + fileNum + DOTXML);
userObj = (User) Quick.getRoot(doc);
}
else if (selectedTestPaper.equalsIgnoreCase("DQ"))
{
DQTestPaper1Schema userSchema = new DQTestPaper1Schema();
QDoc doc = parseInputXML(userSchema, XMLFILEPATH_DQ + fileNum + DOTXML);
userObj = (User) Quick.getRoot(doc);
}
else
{
httpSession.setAttribute("testLoginError", "Un-Identified test paper type");
throw new Exception("Un-Identified test paper type");
}
if ( userObj == null)
{
httpSession.setAttribute("testLoginError", "Unable to create test paper, Please contect your site admin");
throw new Exception("Unable to create test paper, Please contect your site admin");
}
httpSession.setAttribute("currQuestion","0");
httpSession.setAttribute("dataBeanObj", userObj);
httpSession.setAttribute("maxSeconds",userObj.getTotSeconds());
httpSession.setAttribute("remainingSeconds",userObj.getTotSeconds());
}
catch (Exception ex)
{
ex.printStackTrace();
valid = false;
}
return valid;
}
public QDoc parseInputXML(Object schemaObject, String xmlFilePath) throws Exception
{
QDoc doc = null;
try
{
Class schemaClass = schemaObject.getClass();
Method schemaMethod = schemaClass.getMethod("createSchema",null);
QDoc schemaInstance = (QDoc) schemaMethod.invoke(schemaClass,null);
//doc = Quick.parse(schemaInstance, xmlFilePath);
ClassLoader cl = this.getClass().getClassLoader();
InputStream is = cl.getResourceAsStream(xmlFilePath);
InputSource inputSource = new InputSource(is);
doc = Quick.parse(schemaInstance, inputSource);
}
catch (Exception ex)
{
throw new Exception (ex.getMessage());
}
return doc ;
}
private boolean validateInput(HttpSession httpSession, String userName,String emailId,String selectedTestPaper)
{
boolean valid = true;
StringBuffer errorStr = new StringBuffer(100);
if (userName == null || userName.trim().equals(""))
{
errorStr.append("User name is mandatory field, Please enter user name <br>");
valid = false;
}
if (emailId == null || emailId.trim().equals(""))
{
errorStr.append("Email address is mandatory field, Please enter email address <br>");
valid = false;
}
if (selectedTestPaper == null || selectedTestPaper.trim().equals(""))
{
errorStr.append("Please select proper test paper <br>");
valid = false;
}
if (!valid)
{
httpSession.setAttribute("testLoginError",errorStr.toString());
}
return valid;
}
}
XML文件的开头如下所示 -
ValidateTestLogin的servlet如下::
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app id="WebApp_ID">
有人可以帮我纠正这个错误吗?