JavaScript Ajax解析XML数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript Ajax解析XML数据相关的知识,希望对你有一定的参考价值。

function basicAJAX(file) {//pass a variable into the function
	var request = getHTTPObject();
	if(request){
		request.onreadystatechange = function() {
			parseResponse(request);
		};
		request.open("GET", file, true);//this is where the var is picked up, the location
		request.send(null);
	}
}
function parseResponse(request) {
	if(request.readyState == 4){//waits for the complete before execute.
		if(request.status == 200 || request.status == 304){
			var data = request.responseXML;//!Important <-----------------
			createInfo(data);
		} else {
			alert("Something Broke!");
		}
	}
}
function createInfo(data) {
	var holder = document.getElementById("showDiv");//the holder div
	
	while(holder.hasChildNodes()){
		holder.removeChild(holder.lastChild);
	}
	//grab the info
	var personName = data.getElementsByTagName("name");//!Important <-----------------
	var personPosition = data.getElementsByTagName("position");//!Important <-----------------
	var personEmail = data.getElementsByTagName("email");//!Important <-----------------
	
	var theUL = document.createElement("ul");
	//name
	var nameLI = document.createElement("li");
	var nameLIText = document.createTextNode(personName[0].firstChild.nodeValue);
	nameLI.appendChild(nameLIText);
	theUL.appendChild(nameLI);
	//position
	var positionLI = document.createElement("li");
	var positionLIText = document.createTextNode(personPosition[0].firstChild.nodeValue);
	positionLI.appendChild(positionLIText);
	theUL.appendChild(positionLI);
	//email
	var emailLI = document.createElement("li");
	var emailLIText = document.createTextNode(personEmail[0].firstChild.nodeValue);
	emailLI.appendChild(emailLIText);
	theUL.appendChild(emailLI);
	
	holder.appendChild(theUL);
}

以上是关于JavaScript Ajax解析XML数据的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript实现解析xml文件数据

AJAX学习

Ajax第一课 Ajax访问Servlet解析Json格式

Ajax全解析

Ajax全解析

ajax异步传输之深入解析