首页 > 网站 > WEB开发 > 正文

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

2024-04-27 14:07:05
字体:
来源:转载
供稿:网友

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>

  


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表