日期:2014-05-16  浏览次数:20316 次

javascript解析xml文件(一)
页面文件已经能够包含html和js代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>demo1.html</title>

		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="this is my page">
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">

		<script language="javascript" type="text/javascript">
			var xhai_xml;
			function xhai_GetXML(){
				try{
			 	    xhai_xml = new ActiveXObject("Microsoft.XMLDOM");
				    xhai_xml.async=true;
				    xhai_xml.onreadystatechange = GetReadyIE;
				    xhai_xml.load("datafile.xml");
			 	}catch(Exception){
			 		var record;
					xhai_xml = document.implementation.createDocument("","M2MDoc",null);
					xhai_xml.async=false;
					xhai_xml.load("datafile.xml");
					//alert(xhai_xml.getElementsByTagName("SPID")[0].firstChild.nodeValue);
					record = "SPID:"+xhai_xml.getElementsByTagName("SPID")[0].firstChild.nodeValue+"<br>";
					//alert(xhai_xml.getElementsByTagName("SPName")[0].firstChild.nodeValue);
					record = record + "SPName:"+ xhai_xml.getElementsByTagName("SPName")[0].firstChild.nodeValue + "<br>";
					record = record + "MovieID | MovieName | Class |<br>";
					//alert(xhai_xml.getElementsByTagName("Item").length);
					var items = xhai_xml.getElementsByTagName("Item");
					for(var i = 0 ; i < items.length ; i++){
						//alert(items[i].attributes.length);
						var attributes = items[i].attributes;
						for(var j = 0 ; j < attributes.length ; j++){
							//alert(attributes[j].value);
							record = record + attributes[j].value + "|";
						}
						record = record + "<br>";
					}
					
					var page_attributes = xhai_xml.getElementsByTagName("Page")[0].attributes;
					record = record + "Name:" + page_attributes[0].value+"  ";
					record = record + "AllPage" + page_attributes[1].value+"  ";
					record = record + "Count" + page_attributes[2].value+"  ";	
					document.write(record);
			 	}
			}
			
			function GetReadyIE(){
					 if(xhai_xml.readyState==4){
					     if(xhai_xml.parseError.errorCode==0){
					  		  document.write("SPID:"+xhai_xml.documentElement.selectNodes("SPID")(0).text+"<br />");
					 		  document.write("SPName:"+xhai_xml.documentElement.selectNodes("SPName")(0).text+"<br />");
					 		  document.write("<br />");
					  		  var i,c,d,Line;
					  		  var MovieList = xhai_xml.documentElement.selectNodes("//MovieList/Item");
					 		  var j=MovieList.length;
						      Line = MovieList.item(0);
							  d=Line.attributes.length;
							  for(c=0;c<d;c++){
							  	 document.write(Line.attributes(c).name+" | ");
						 	  }
							  document.write("<br />");
							  for(i=0;i<j;i++){
								  	Line = MovieList.item(i);
								    d=Line.attributes.length;
								    for(c=0;c<d;c++){
								    		document.write(Line.attributes(c).value+" | ");
								    }
								    document.write("<br />");
							   }
							   document.write("<br />");
							   var page = xhai_xml.documentElement.selectNodes("//Page").item(0)
							   j=page.attributes.length;
							   for(i=0;i<j;i++){
								   	document.write(page.attributes(i).name+":");
								   	document.write(page.attributes(i).value+" ");
							   }
							}
							delete(xhai_xml);
					    }
			}
			xhai_GetXML();
		</script>
	</head>

	<body>

	</body>
</html>
datafile.xml
<?xml version="1.0" encoding="gb2312"?>
<M2MDoc>
    <SPID>3</SPID>
    <SPName>5</SPName>
    <MovieList>
        <Item MovieID="103704" MovieName="忍" Class="动作片" />
        <Item MovieID="103701" MovieName="风斗士" Class="动作片" /> 
        <Item MovieID="10369