扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
var xmlHttp;
function createXMLHttpRequest() {
if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function prepareXML() {
var xml = "<skills>";
var options = document.getElementById("skills").childNodes;
for(var i = 0; i < options.length; i++) {
var option = options[i];
if(option.selected) {
xml = xml + "<skill>" + option.value + "<\/skill>";
}
}
xml = xml + "<\/skills>";
return xml;
}
function handleSkills() {
var xml = prepareXML();
var url = "XMLServlet?timeStamp" + new Date().getTime();
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("POST", url);
xmlHttp.setRequestHeader("Content-Type", "text/xml");
xmlHttp.send(xml);
}
package onlyfun.caterpillar;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XMLServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String xml = readXMLFromRequestBody(request);
Document xmlDoc = null;
try {
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
xmlDoc = builder.parse(
new ByteArrayInputStream(xml.getBytes()));
}
catch(ParserConfigurationException e) {
System.out.println(e);
}
catch(SAXException e) {
System.out.println(e);
}
String responseXML = prepareXMLResponse(xmlDoc);
responseXML = responseXML + "</responses>";
response.setContentType("text/xml");
response.getWriter().print(responseXML);
}
private String readXMLFromRequestBody(HttpServletRequest request){
StringBuffer xml = new StringBuffer();
try {
BufferedReader reader = request.getReader();
String line = null;
while((line = reader.readLine()) != null) {
xml.append(line);
}
}
catch(Exception e) {
System.out.println("XML讀取有誤…" + e.toString());
}
return xml.toString();
}
private String prepareXMLResponse(Document xmlDoc) {
NodeList selectedSkills = xmlDoc.getElementsByTagName("skill");
StringBuffer xml = new StringBuffer();
xml.append("<responses>");
for(int i = 0; i < selectedSkills.getLength(); i++) {
String skill = selectedSkills.item(i).getFirstChild().getNodeValue();
xml.append("<response>");
xml.append(skill);
xml.append("</response>");
}
return xml.toString();
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>處理XML</title>
<script type="text/javascript" src="HandleXMLEx-1.js"></script>
</head>
<body>
<select id="skills" size="5" multiple="true">
<option value="c">C 語言</option>
<option value="cpp">C++</option>
<option value="java">Java</option>
<option value="csharp">C#</option>
<option value="vb">VB</option>
</select>
<br>
<input type="button" value="送出" onclick="handleSkills();"/>
<br>
<div id="response"></div>
</body>
</html>
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者