用jsp将xml文件解析到网页显示,并把数据提交保存到数据库
2012-09-18 13:34:10 来源:WEB开发网核心提示:Document document=questionDB.parse(pageContext.getServletContext().getResourceAsStream ("question.xml"));Element element = document.getDocumentElement
Document document=questionDB.parse(pageContext.getServletContext().getResourceAsStream ("question.xml"));
Element element = document.getDocumentElement();
NodeList questions = element.getElementsByTagName("question");
%>
<form name="questionform" id="questionform" action="questionsave.jsp" method="POST">
<%
out.println("<input type="+"\"hidden\""+" name="+"\"question_id\""+" value="+"\""+element.getAttribute("id")+"\""+"/>");
for (int i = 0; i < questions.getLength(); i++) {
Element questionElement = (Element) questions.item(i);
if(Integer.parseInt(questionElement.getAttribute("type"))==1){
NodeList childNodes = questionElement.getChildNodes();
for (int j = 0; j< childNodes.getLength(); j++) {
//Element answerElement = (Element)answers.item(j);
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
if ("title".equals(childNodes.item(j).getNodeName())) {
out.println("<h3>"+questionElement.getAttribute("id")+"、"+childNodes.item(j).getFirstChild().getNodeValue()+"</h3>");
//out.println("<input type="+"\"hidden\""+" name="+"\"question_single_"+questionElement.getAttribute("id")+"\""+" value="+"\""+questionElement.getAttribute("id")+"\""+"/>");
}else if ("answer".equals(childNodes.item(j).getNodeName())) {
NodeList itemNodes=childNodes.item(j).getChildNodes();
for(int k=0;k<itemNodes.getLength();k++){
//Element itemElement=(Element)itemNodes.item(k);
if(itemNodes.item(k).getNodeType()==Node.ELEMENT_NODE){
if("it".equals(itemNodes.item(k).getNodeName())){
out.println("<input name="+"\"sing_"+questionElement.getAttribute("id")+"_"+questionElement.getAttribute("type")+"\" "+" type="+"\"radio\""+" value="+"\""
+itemNodes.item(k).getFirstChild().getNodeValue()+"\""+">"+itemNodes.item(k).getFirstChild().getNodeValue()+"</input>");
}
}
}
}
}
}
}
if(Integer.parseInt(questionElement.getAttribute("type"))==2){
NodeList childNodes = questionElement.getChildNodes();
for (int j = 0; j< childNodes.getLength(); j++) {
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
if ("title".equals(childNodes.item(j).getNodeName())) {
out.println("<h3>"+questionElement.getAttribute("id")+"、"+childNodes.item(j).getFirstChild().getNodeValue()+"</h3>");
Element element = document.getDocumentElement();
NodeList questions = element.getElementsByTagName("question");
%>
<form name="questionform" id="questionform" action="questionsave.jsp" method="POST">
<%
out.println("<input type="+"\"hidden\""+" name="+"\"question_id\""+" value="+"\""+element.getAttribute("id")+"\""+"/>");
for (int i = 0; i < questions.getLength(); i++) {
Element questionElement = (Element) questions.item(i);
if(Integer.parseInt(questionElement.getAttribute("type"))==1){
NodeList childNodes = questionElement.getChildNodes();
for (int j = 0; j< childNodes.getLength(); j++) {
//Element answerElement = (Element)answers.item(j);
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
if ("title".equals(childNodes.item(j).getNodeName())) {
out.println("<h3>"+questionElement.getAttribute("id")+"、"+childNodes.item(j).getFirstChild().getNodeValue()+"</h3>");
//out.println("<input type="+"\"hidden\""+" name="+"\"question_single_"+questionElement.getAttribute("id")+"\""+" value="+"\""+questionElement.getAttribute("id")+"\""+"/>");
}else if ("answer".equals(childNodes.item(j).getNodeName())) {
NodeList itemNodes=childNodes.item(j).getChildNodes();
for(int k=0;k<itemNodes.getLength();k++){
//Element itemElement=(Element)itemNodes.item(k);
if(itemNodes.item(k).getNodeType()==Node.ELEMENT_NODE){
if("it".equals(itemNodes.item(k).getNodeName())){
out.println("<input name="+"\"sing_"+questionElement.getAttribute("id")+"_"+questionElement.getAttribute("type")+"\" "+" type="+"\"radio\""+" value="+"\""
+itemNodes.item(k).getFirstChild().getNodeValue()+"\""+">"+itemNodes.item(k).getFirstChild().getNodeValue()+"</input>");
}
}
}
}
}
}
}
if(Integer.parseInt(questionElement.getAttribute("type"))==2){
NodeList childNodes = questionElement.getChildNodes();
for (int j = 0; j< childNodes.getLength(); j++) {
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
if ("title".equals(childNodes.item(j).getNodeName())) {
out.println("<h3>"+questionElement.getAttribute("id")+"、"+childNodes.item(j).getFirstChild().getNodeValue()+"</h3>");
更多精彩
赞助商链接