用jsp将xml文件解析到网页显示,并把数据提交保存到数据库
<?xml version="1.0" encoding="UTF-8"?><questions id="1">
<question id="1" type="1">
<title><![CDATA[你是哪个国家的人?]]></title>
<answer>
<it><![CDATA[1、中 国]]></it>
<it><![CDATA[2、日 本]]></it>
<it><![CDATA[3、韩 国]]></it>
<it><![CDATA[4、其 他]]></it>
</answer>
</question>
<question id="3" type="2">
<title><![CDATA[你是什么样的人?]]></title>
<answer>
<it><![CDATA[1、文 静]]></it>
<it><![CDATA[2、干 净]]></it>
<it><![CDATA[3、豪 爽]]></it>
<it><![CDATA[4、不 丑]]></it>
</answer>
</question>
<question id="4" type="2">
<title><![CDATA[你是什么样的人?]]></title>
<answer>
<it><![CDATA[1、文 静]]></it>
<it><![CDATA[2、干 净]]></it>
<it><![CDATA[3、豪 爽]]></it>
<it><![CDATA[4、不 丑]]></it>
</answer>
</question>
<question id="5" type="3">
<title><![CDATA[你怎样评价自己?]]></title>
<answer>
<it><![CDATA[例如,我是一个很牛逼的人!]]></it>
</answer>
</question>
<question id="6" type="3">
<title><![CDATA[你怎样评价自己?]]></title>
<answer>
<it><![CDATA[例如,我是一个很牛逼的人!]]></it>
</answer>
</question>
</questions>
questions.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="javax.xml.parsers.*,org.w3c.dom.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>QUESTION</title>
</head>
<body bgcolor="#79CDCD">
<%
DocumentBuilderFactory questionDBF=DocumentBuilderFactory.newInstance();
DocumentBuilder questionDB = questionDBF.newDocumentBuilder();
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())){<
补充:Web开发 , Jsp ,