`

js加载xml

    博客分类:
  • XML
阅读更多

1、Server.CreateObject("Microsoft.XMLHTTP"),浏览器中,自带的对象,就是一个处理请求返回xml对象的对象,Readystate是他的一个状态,是ajax的基础。利用它可以往后台的对象发送请求,并且可以接收返回,然后动态改变网页,达到很好用户体验。

2、Microsoft.XMLDOM相关资料是装载一个xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 如果同时设置了encoding和standalone属性,standalone属性要位于encoding属性之后 -->
<book>
	<author>小定</author>
	<title>java web大全</title>
	<price>$5</price>
</book>




/*JavaScript加载XML文件*/
function loadXMLDoc(dname){
	try{
		//通过微软的XML解析器加载XML文件
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");//创建空的微软XML文档对象
		
	}catch(e){
		try{
			//通过其他浏览器的XML解析器加载XML文件
			xmlDoc = document.implementation.createDocument("","",null);//创建空的XML文档对象
			
		}catch(e){alert(e.message)}
	}
	try{
		xmlDoc.async = false;//关闭异步加载
		xmlDoc.load(dname);//加载名为"xxx.xml"的文档
		return(xmlDoc);
	}catch(e){alert(e.message)}
	return(null);
	
}

function loadXMLDocByHttp(dname){
	
	if(window.XMLHttpRequest){
	
		xhttp = new XMLHttpRequest();//其他浏览器,创建空的XMLHTTP对象(包括IE7+)
		
	}else{
	
		xhttp = new ActiveXObject("Microsoft.XMLHTTP");//创建空的微软XMLHTTP对象(老版本的IE5和IE6)	
	}
	xhttp.open("GET",dname,false);//打开XMLHTTP对象
	xhttp.send();//发送XMLHTTP对象
	xmlDoc =  xhttp.responseXML;//获得XMLDoc对象
	return xmlDoc;
}

/*加载XML字符串*/
function loadXMLString(txt){

	if(window.DOMParser){//其他浏览器
	
		parser = new DOMParser();
		xmlDoc = parser.parseFromString(txt,"text/xml");//创建空的XML文档对象
		
	}else{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");//创建空的微软XML文档对象
		xmlDoc.async = "false";//关闭异步加载
		xmlDoc.loadXML(txt);
	}

	return xmlDoc;

}



<%@ 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 'index.jsp' 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">
	-->
	<script type="text/javascript" src="loadxmldoc.js"></script>
  </head>
  
  <body>
    <script type="text/javascript">
		xmlDoc=loadXMLDoc("book.xml");
		xmlDocHttp = loadXMLDocByHttp("book.xml");
		
		document.write("xmlDoc is loaded, ready for use");
		document.write("xmlDocHttp is loaded, ready for use");
		document.write("</br>");
		
		var arr = xmlDoc.getElementsByTagName("title");
		for(var i=0;i<arr.length;i++){
			var oXMLNode = arr[i];
			document.write("节点名称: "+oXMLNode.nodeName);
			document.write("</br>");
			document.write(oXMLNode.text);
			document.write("</br>");
			document.write(oXMLNode.nextSibling.nodeName+"  ");//返回下一个同级节点
			document.write(oXMLNode.previousSibling.nodeName);//返回前一个兄弟节点
			document.write("</br>");
			document.write(oXMLNode.parentNode.nodeType+"  ");//父节点
			document.write(oXMLNode.parentNode.nodeName+"  ");
			document.write(oXMLNode.parentNode.nodeValue);
			document.write("</br>");
			var arrXMLNodes = oXMLNode.childNodes;//子节点数组
			for(var j=0;j<arrXMLNodes.length;j++){
				document.write(arrXMLNodes[j].nodeName);
				document.write("</br>");
				document.write(arrXMLNodes[j].nodeType);
				document.write("</br>");
				document.write(arrXMLNodes[j].nodeValue);
			}
			
		}

	</script>
  </body>
</html>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics