xml简单应用

/*
* 将字符串转化成XMLDom对象 
* @param xmlStr xml字符串
*/
function createXmlDOM(xmlStr){
	if(document.all){//IE
		var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
		xmlDom.loadXML(xmlStr);
		return xmlDom;
	}else{//other
		return new DOMParser().parseFromString(xmlStr,"text/xml");
	}
}
//用例 xmlstr 是一段标准xml字符串
/*
<?xml version="1.0" encoding="UTF-8" ?> 
<services>
 <process>
	<exception>登录密码不正确!</exception> 
 </process>
</services>


<?xml version="1.0" encoding="UTF-8" ?> 
<services>
 <process>
   <params>
	  <param name="username">管理员</param> 
	  <param name="departName">公安部</param> 
   </params>
  </process>
</services>

*/

var dataXML=createXmlDOM(xmlstr);
var result = true; 
$(dataXML).find("exception").each(function(){       
		alert($(this).text());
		result = false;//TODO 标记登录失败
});
if(result){
	$(dataXML).find("param").each(function(){       
		if("username"== $(this).attr("name") ){
			document.getElementById("span_uid").innerHTML ="当前用户:"+ ($(this).text());
			login_win.close();
		}
	});
}
 

你可能感兴趣的:(xml)