Home > Web Front-end > JS Tutorial > body text

Ajax method to traverse xml document

亚连
Release: 2018-05-25 10:34:59
Original
1717 people have browsed it

This article mainly introduces the method of ajax traversing XML documents. It analyzes the implementation techniques of jsp combined with Ajax traversing XML documents through examples. It has certain reference value. Friends who need it can refer to it.

The examples of this article are described Ajax method for traversing xml documents. Share it with everyone for your reference. The specific analysis is as follows:

The XMLHttpRequest object provides two attributes that can be used to access the server response. The first attribute, responseText, provides the response as a string, and the second attribute, responseXML, provides the response as an XML object. Some simple use cases are suitable for getting the response by simple text, such as displaying the response in an alert box, or the response is just a word indicating success or failure
The example in the previous was obtained from the XMLHttpRequest object The server responds and gets the response as text using the responseText property of the XMLHttpRequest object.
This time we will use the responseXML attribute of the XMLHttpRequest object to obtain the result as an XML document. In this way, we can use the W3C DOM method to traverse the XML document. (The previous article has more or less talked about DOM, so I won’t repeat it here)

OK, let’s look at the example.

First of all, there is a piece of XML document code (parseXML.xml) as follows:

parseXML.xml is as follows:


<?xml version="1.0" encoding="UTF-8"?>
<states>
<north>
<state>Minnesota</state>
<state>Iowa</state>
<state>North Dakota</state>
</north>
<south>
<state>Texas</state>
<state>Oklahoma</state>
<state>Louisiana</state>
</south>
<east>
<state>New York</state>
<state>North Carolina</state>
<state>Massachusetts</state>
</east>
<west>
<state>California</state>
<state>Oregon</state>
<state>Nevada</state>
</west>
</states>
Copy after login


MyJsp.jsp is as follows:


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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%>">
<title>My JSP &#39;MyJsp.jsp&#39; starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0"> 
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<script type="text/javascript">
var flg=false;
var requestType = "";
//得到XMLHttpRequest对象
function newXMLHttpRequest() {
var xmlreq = false;
if (window.XMLHttpRequest) {
xmlreq = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) {
try {
xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {}
}
}
return xmlreq;
}
function startRequest(requestedList){
requestType=requestedList;
flg=newXMLHttpRequest();
//当XMLHttpRequest对象在请求过程中间状态改变的时候
//回来调用handleStateChange方法
flg.onreadystatechange = handleStateChange;
flg.open("GET", "parseXML.xml", true);
flg.send(null);
}
//处理函数
function handleStateChange(){
if(flg.readyState==4){
if(flg.status==200){
if(requestType=="north"){
listNorthStates();
}else if(requestType=="all"){
listAllStates();
}if(requestType=="south"){
listSouthStates();
}
}
}
}
//用于显示NorthStates方法
function listNorthStates(){
var xmlDoc=flg.responseXML;
var northNode=xmlDoc.getElementsByTagName("north")[0];
var northStates=northNode.getElementsByTagName("state");
outputList("North States",northStates);
}
//用于显示SouthStates方法
function listSouthStates(){
var xmlDoc=flg.responseXML;
var SouthNode=xmlDoc.getElementsByTagName("south")[0];
var SouthStates=SouthNode.getElementsByTagName("state");
outputList("South States",SouthStates);
}
//用于显示AllStates方法
function listAllStates(){
var xmlDoc=flg.responseXML;
var allStates=xmlDoc.getElementsByTagName("state");
outputList("All States in Document", allStates);
}
//输出元素并显示于提示框中
function outputList(title,states){
var out=title;
var currState=null;
for(var i=0;i<states.length;i++){
currState=states;
out=out+"\n-"+currState.childNodes[0].nodeValue;
}
alert(out);
}
</script>
<body>
<form action="#">
<input type="button" value="View All Listed States"
  onclick="startRequest(&#39;all&#39;);"/><br>
<input type="button" value="View All Listed Northern States"
  onclick="startRequest(&#39;north&#39;);"/><br>
<input type="button" value="View All Listed Southern States"
  onclick="startRequest(&#39;south&#39;);"/>
</form>
</body>
</html>
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Ajax upload method to implement js processing based on server-side return data

Double-layer ajax embedding Set (can be multi-layered) usage examples

Ajax implementation of pop-up non-refresh city selection function code

The above is the detailed content of Ajax method to traverse xml document. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template