`
yiyanwan77
  • 浏览: 183576 次
  • 性别: Icon_minigender_1
  • 来自: 威海
社区版块
存档分类
最新评论

js解析xml

阅读更多
<?xml version="1.0" encoding="UTF-8"?>
<humans>
 <human id="linghuchong" id2="huchongling">
  <name id="name">令狐冲</name>
 </human>
</humans>

 

function MayXML(url,callback){
	var self=this;
	this.xmlDoc=null;
	
	if(window.ActiveXObject)
    {
        self.xmlDoc= new ActiveXObject('Microsoft.XMLDOM');
        self.xmlDoc.async    = false;
		self.xmlDoc.onreadystatechange=function(){
			if(self.xmlDoc.readyState==4){
				May.exe(self.callback,self);
			}
		};
    }
    else if (document.implementation&&document.implementation.createDocument)
    {
       self.xmlDoc= document.implementation.createDocument('', '', null);
        self.xmlDoc.onload=function(){
			May.exe(self.callback,self);
			};
    }
	
	this.url=url;
	this.callback=callback;
	this.load=function(){
		self.xmlDoc.load(url);
	};
	this.getXmlDoc=function(){
		return self.xmlDoc;
	};
};

 

 

var mayxml1=new MayXML("customXML.xml",function(mayXML){
						alert("载入成功 ");
						//alert(xmlDoc.getElementsByTagName("human")[0].getAttribute("id"));
						//alert(xmlDoc.getElementsByTagName("*")[0].childNodes[0].getAttribute("id"));
						//alert(mayXML.getXmlDoc().getElementsByTagName("human")[0].getAttribute("id"));
						alert(mayXML.getXmlDoc().selectNodes("humans/human[@id='linghuchong']")[0].childNodes[0].text);
						});
				mayxml1.load();

 

  //注意,此处selectNodes仅ie支持

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics