首页 > Js/Jquery

XML DOM操作,适用目前流行的浏览器

发表于2015-07-14 14:41:54| --次阅读| 来源webkfa| 作者js,javascript

摘要:
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title></title>
	<link rel="stylesheet" href="">
	<script>
	function loadXMLString(txt) {
	    var xmlDoc = null;

	    if (window.DOMParser) {
	        var parser = new window.DOMParser();
	        xmlDoc = parser.parseFromString(txt, "text/xml");
	    }
	    else // code for IE
	    {
	        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	        xmlDoc.async = false;
	        xmlDoc.loadXML(txt);
	    }

	    return xmlDoc;
	}


	var s = '<alarm sound="I" locate="I"><alarm_out><ctl id="1" no="11" val="111"/><ctl id="2" no="22" val="222"/><ctl id="3" no="33" val="333"/></alarm_out><camera><id>c1</id><id>c2</id><id>c3</id></camera></alarm>';

	var xml = loadXMLString(s);
	var c = xml.getElementsByTagName("alarm")[0];

	var alarm_out = c.getElementsByTagName("alarm_out")[0];
	if(alarm_out){
		for (var i = 0; i < alarm_out.childNodes.length; i++) {
			console.log('==========='+(i+1)+'===========');
			console.log("alarm_out attribute id:" + alarm_out.childNodes[i].getAttribute("id"));
			console.log("alarm_out attribute no:" + alarm_out.childNodes[i].getAttribute("no"));
			console.log("alarm_out attribute val:" + alarm_out.childNodes[i].getAttribute("val"));
			console.log('==========='+(i+1)+'===========');
		}
	}

	var camera = c.getElementsByTagName("camera")[0];
	if(camera){
		for (var i = 0; i < camera.childNodes.length; i++) {
			var a = camera.childNodes[i];
			console.log("camera id:" + camera.childNodes[i].textContent);
		}
	}
	</script>
</head>
<body>
	
</body>
</html>

  

相关文章

猜你喜欢

学到老在线代码浏览器 关闭浏览
友情链接: hao123 360导航 搜狗网址导航 114啦网址导航 博客大全
Copyright © 1999-2014, WEBKFA.COM, All Rights Reserved  京ICP备14034497号-1