码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-13 11:51:23      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

<!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>

  

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

标签:

原文地址:http://www.cnblogs.com/numver/p/4642297.html

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