码迷,mamicode.com
首页 > Web开发 > 详细

js解析xml,获取XMl标签属性值

时间:2016-06-19 11:24:35      阅读:461      评论:0      收藏:0      [点我收藏+]

标签:

<script type="text/javascript">
var xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+"<RecordInfo camId=\"1000000$1$0$0\" count=\"12\" />";


$(function(){
//加载xmlDoc
var xmlDoc=loadXML(xml);
//获取xml指定标签
var tag=xmlDoc.getElementsByTagName("RecordInfo")[0];
//获取标签指定属性值
var attr=tag.getAttribute("count");
alert(attr);
});
var loadXML = function(xmlString)
{
var xmlDoc=null;

if(!window.DOMParser && window.ActiveXObject)
{
var xmlDomVersions = [‘MSXML.2.DOMDocument.6.0‘,‘MSXML.2.DOMDocument.3.0‘,‘Microsoft.XMLDOM‘];
for(var i=0;i<xmlDomVersions.length;i++)
{
try
{
xmlDoc = new ActiveXObject(xmlDomVersions[i]);
xmlDoc.async = false;
xmlDoc.loadXML(xmlString); //loadXML方法载入xml字符串
break;
}
catch(e)
{
}
}
}
else if(window.DOMParser && document.implementation && document.implementation.createDocument)
{
try
{
domParser = new DOMParser();
xmlDoc = domParser.parseFromString(xmlString, ‘text/xml‘);
}
catch(e)
{
}
}
else
{
return null;
}

return xmlDoc;
}
</script>

js解析xml,获取XMl标签属性值

标签:

原文地址:http://www.cnblogs.com/shaohaixiong/p/5597531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!