ajax或者jquery操作xml相关源文件
/*通过异步传输XMLHTTP发送参数到ajaxServlet,返回符合条件的XML文档*/ var xhr; function createXHR() { if (typeof XMLHttpRequest != "undefined") {//检查原生XHR对象是否存在,如果存在则返回她的实例 return new XMLHttpRequest(); } else if (typeof ActiveXObject != "undefined") {//如果原生的XHR不存在则检查ActiveX对象 var versions = [ "MSXML2.XMLHttp.6.0", " ;MSXML2.XMLHttp.3.0", "MLXML2.XMLHttp" ]; for ( var i = 0, len = versions.length; i < len; i++) { try { var xhr = new ActiveXObject(versions[i]); arguments.callee.activeXString = versions[i]; return xhr; } catch (ex) { //跳过 } } return new ActiveXObject(arguments.callee.activeXString); } else {//如果两种对象都不存在则报错误 throw new Error("没有有效的XHR对象"); } } function getResult() { var url = "Test?action=send"; xhr = createXHR(); xhr.open("GET", url, false); xhr xhr.send(null); } /*分析返回的XML文档*/ function complete() { if (xhr.readyState == 4) { if (xhr.status == 200) { var type = xhr.responseXML.getElementsByTagName("person"); /*result=type[0].childNodes[0].data; alert(result);*/ try { var info = xhr.responseXML.getElementsByTagName("type"); alert(info[0].childNodes[0].nodeName);//获取节点名 alert(info[0].childNodes[0].attributes.getNamedItem("name").value);//获取属性值 alert(info[0].childNodes[0].childNodes[0].nodeName); // alert(info[0].childNodes[1].childNodes[0].getAttribute("")); alert(info[0].childNodes[0].childNodes[0].childNodes[0].nodeValue);//获取节点值 //alert(info.getAttribute('type')); } catch (exception) { alert("The node is not exist"); } var str = new Array(); //alert(type.length); for ( var i = 0; i < (type.length+1); i++) { str[i] = type[0].childNodes[i].firstChild.data;//获取节点值 document.all['td'][removed] += str[i] + ""; } } } } ;MSXML2.XMLHttp.3.0", "MLXML2.XMLHttp" ]; for ( var i = 0, len = versions.length; i < len; i++) { try { var xhr = new ActiveXObject(versions[i]); arguments.callee.activeXString = versions[i]; return xhr; } catch (ex) { //跳过 } } return new ActiveXObject(arguments.callee.activeXString); } else {//如果两种对象都不存在则报错误 throw new Error("没有有效的XHR对象"); } } function getResult() { var url = "Test?action=send"; xhr = createXHR(); xhr.open("GET", url, false); xhr xhr.send(null); } /*分析返回的XML文档*/ function complete() { if (xhr.readyState == 4) { if (xhr.status == 200) { var type = xhr.responseXML.getElementsByTagName("person"); /*result=type[0].childNodes[0].data; alert(result);*/ try { var info = xhr.responseXML.getElementsByTagName("type"); alert(info[0].childNodes[0].nodeName);//获取节点名 alert(info[0].childNodes[0].attributes.getNamedItem("name").value);//获取属性值 alert(info[0].childNodes[0].childNodes[0].nodeName); // alert(info[0].childNodes[1].childNodes[0].getAttribute("")); alert(info[0].childNodes[0].childNodes[0].childNodes[0].nodeValue);//获取节点值 //alert(info.getAttribute('type')); } catch (exception) { alert("The node is not exist"); } var str = new Array(); //alert(type.length); for ( var i = 0; i < (type.length+1); i++) { str[i] = type[0].childNodes[i].firstChild.data;//获取节点值 document.all['td'][removed] += str[i] + ""; } } } }
用户评论
很好,可以使用。学习了。