javascript 解析xml (chrome,firefox,safari,Opera,ie)

注:不允许任何形式的转载
  	//loadXMl
  	function loadXml(xmlFile){
  		var xmlDoc;
  		if(window.ActiveXObject){	//IE
  			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  			xmlDoc.async=false;
  			xmlDoc.load(xmlFile);
  		}else if(document.implementation && document.implementation.createDocument){//firefox  opera
	  		try{
			  		xmlDoc = document.implementation.createDocument("", "", null);  
					xmlDoc.async = false;  
					xmlDoc.load(xmlFile);
			}catch(e){
				  try //Google Chrome  safari
				  {    
					   var xmlhttp = new window.XMLHttpRequest();  
					   xmlhttp.open("GET",xmlFile,false);    
					   xmlhttp.send(null);    
					   xmlDoc = xmlhttp.responseXML;  
				  }    
				  catch(e)    
				  {    
				   error=e.message;    
				   alert(error);  
				  } 
			}  
  		}else{
  			return null;
  		}
  		return xmlDoc;
  	}
	//checkXMLDocObj
	function chechXMLDocObj(xmlFile){
		var xmlDoc=loadXml(xmlFile);
		if(null==xmlDoc){
			alert("浏览器不支持");
			return false;
		}
		return xmlDoc;
	}
		//$
		var $=function(id){
			 return document.getElementById(id); 
		}
		function loadIbookXml(){
		var xmlDoc=chechXMLDocObj("ibook.xml");
		var root=xmlDoc.documentElement;
		//book下的接点
		var x=root.childNodes;
		//	alert(root.getElementsByTagName('book')[0].childNodes);
		var update= (null == root.getElementsByTagName('update')[0])?"":root.getElementsByTagName('update')[0].firstChild.data;
		var name = (null == root.getElementsByTagName('name')[0])?"":root.getElementsByTagName('name')[0].firstChild.data;	
		var title = (null == root.getElementsByTagName('title')[0])?"":root.getElementsByTagName('title')[0].firstChild.data;
		var Keywords = (null == root.getElementsByTagName('Keywords')[0])?"":root.getElementsByTagName('Keywords')[0].firstChild.data;
		var description = (null == root.getElementsByTagName('description')[0])?"":root.getElementsByTagName('description')[0].firstChild.data;	
		var contents_size = (null == root.getElementsByTagName('contents_size')[0])?"":root.getElementsByTagName('contents_size')[0].firstChild.data;		
		var total = (null == root.getElementsByTagName('total')[0])?0:parseInt(root.getElementsByTagName('total')[0].firstChild.data);		
		
		var w=null,h=null;
		/*遍历div接点*/
		for(var i=0 ;i<x.length;i++){
			if("div"==x[i].nodeName){
				//div 接点的子节点
				var d=root.getElementsByTagName('div')[0].childNodes;
				/*得到w,h的值*/
				for(var j=0 ; j<d.length;j++){
					if("w" == d[j].nodeName){
						w = d[j].firstChild.data;	
					}else if("h"==d[j].nodeName){
						h = d[j].firstChild.data;
						break;
					}
				}//for-w,h
			}
		}//for()
		w=(null==w)?1024:w;
		h=(null==h)?768:h;
		
		
		var zoom_s=(null == root.getElementsByTagName('zoom_s')[0]) ? "" : root.getElementsByTagName('zoom_s')[0].firstChild.data;
		var over_zoom=(null==root.getElementsByTagName('over_zoom')[0])?"":root.getElementsByTagName('over_zoom')[0].firstChild.data;
		
		var pagelink_color=(null==root.getElementsByTagName('pagelink_color')[0])?"":root.getElementsByTagName('pagelink_color')[0].firstChild.data;
		var pagelink_highlight=(null==root.getElementsByTagName('pagelink_highlight')[0])?"":root.getElementsByTagName('pagelink_highlight')[0].firstChild.data;
		var pagesearch_color=(null==root.getElementsByTagName('pagesearch_color')[0])?"":root.getElementsByTagName('pagesearch_color')[0].firstChild.data;
		var pagesearch_color_selected=(null==root.getElementsByTagName('pagesearch_color_selected')[0])?"":root.getElementsByTagName('pagesearch_color_selected')[0].firstChild.data;
		var point_scale_w=(null==root.getElementsByTagName('point_scale_w')[0])?"":root.getElementsByTagName('point_scale_w')[0].firstChild.data;
		var point_scale_h=(null==root.getElementsByTagName('point_scale_h')[0])?"":root.getElementsByTagName('point_scale_h')[0].firstChild.data;
	
		/*true false*/
		var toright=(null!=xmlDoc.getElementsByTagName("toright"))? true:false;
		var cover=(null!=xmlDoc.getElementsByTagName("cover"))?true:false;
		var streaming=(null!=xmlDoc.getElementsByTagName("streaming"))?true:false;
		var download=(null!=xmlDoc.getElementsByTagName("download"))?true:false;
		var textonly=(null!=xmlDoc.getElementsByTagName("textonly"))?true:false;
		var search=(null!=xmlDoc.getElementsByTagName("search"))?true:false;
		var pen=(null!=xmlDoc.getElementsByTagName("pen"))?true:false;
		
		document.write("update:"+update+"<br/>"
		+"\n"+"name:"+name	+"<br/>"
		+"\n"+"title:"+title+"<br/>"
		+"\n"+"total:"+total+"<br/>"
		+"\n"+"description:"+description+"<br/>"
		+"\n"+"contents_size:"+contents_size+"<br/>"
		+"\n"+"w:"+w+"<br/>"
		+"\n"+"h:"+h+"<br/>"
		+"\n"+"zoom_s:"+zoom_s+"<br/>"
		+"\n"+"over_zoom:"+over_zoom+"<br/>"
		+"\n"+"pagelink_color:"+pagelink_color+"<br/>"
		+"\n"+"pagelink_highlight:"+pagelink_highlight+"<br/>"
		+"\n"+"pagesearch_color:"+pagesearch_color+"<br/>"
		+"\n"+"pagesearch_color_selected:"+pagesearch_color_selected+"<br/>"
		+"\n"+"toright:"+toright+"<br/>"
		+"\n"+"cover:"+cover+"<br/>"
		+"\n"+"streaming:"+streaming+"<br/>"
		+"\n"+"download:"+download+"<br/>"
		+"\n"+"textonly:"+textonly+"<br/>"
		+"\n"+"search:"+search+"<br/>"
		+"\n"+"pen:"+pen
		);
	}



XML
<?xml version="1.0" encoding="UTF-8"?>
<book>
<update>123456</update>
  <name>asdfghjk</name>
  <title>abm</title>
  <total>45</total>
  <description>abc</description>
  <contents_size>78G</contents_size>
  <toright/>
  <cover/>
  <div>
    <w>789</w>
    <h>659</h>
  </div>
  <zoom_s>1,2</zoom_s>
  <over_zoom>5</over_zoom>
  <pagelink_color>000255255</pagelink_color>
  <pagelink_highlight>2</pagelink_highlight>
  <pagesearch_color>255000255</pagesearch_color>
  <pagesearch_color_selected>255000000</pagesearch_color_selected>
  <point_scale_w>2.4</point_scale_w>
  <point_scale_h>2.1826</point_scale_h>
  <streaming/>
  <download/>
  <textonly/>
  <search/>
  <pen/>
</book>


注:不允许任何形式的转载

你可能感兴趣的:(JavaScript,xml,浏览器)