XML DOM操作,适用目前流行的浏览器
摘要:
<!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>
相关文章
最新发布
阅读排行
热门文章
猜你喜欢