js对xml进行增删改查

stu.xml 文件

<?xml version="1.0" encoding="utf-8" ?>
<student>
  <stu id="1">
    <stuName>张三</stuName>
    <stuAge>20</stuAge>
    <stuSex>男</stuSex>
  </stu>
  <stu id="2">
    <stuName>李四</stuName>
    <stuAge>18</stuAge>
    <stuSex>男</stuSex>
  </stu>
  <stu id="3">
    <stuName>王五</stuName>
    <stuAge>22</stuAge>
    <stuSex>男</stuSex>
  </stu>
  <stu id="4">
    <stuName>赵六</stuName>
    <stuAge>20</stuAge>
    <stuSex>男</stuSex>
  </stu>
</student>

DOM 对xml进行解析(增删改查)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
    //获取xml对象
        var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
        //是否异步获取xml对象
        xmldoc.async = false;
        //加载xml
        xmldoc.load("stu.xml");
//        var node1 = xmldoc.getElementsByTagName("stuName")[0];
        //        alert(node1.childNodes[0].nodeValue);
//        var node1 = xmldoc.getElementsByTagName("stu")[0];
//        for (var i = 0; i < node1.childNodes.length; i++) {
//            document.write(node1.childNodes[i].nodeName + ":" + node1.childNodes[i].text);
        //        }
//        var node1 = xmldoc.getElementsByTagName("stu");
//        for(var i=0;i<node1.length;i++) {
//            for(var j=0;j<node1[i].childNodes.length;j++) {
//                document.write(node1[i].childNodes[j].nodeName + ":" + node1[i].childNodes[j].text+"<br/>");
//            }
//            document.write("======<br/>");
//        }
        //简单方法
        var node2 = xmldoc.getElementsByTagName("student")[0];
        for (var i = 0; i < node2.childNodes.length; i++) {
            var nodes = node2.childNodes[i].childNodes;
            document.write("id:"+node2.childNodes[i].attributes[0].text+"<br/>");
            document.write("stuName:" + nodes[0].childNodes[0].xml + "<br>");
            document.write("stuName:" + nodes[1].childNodes[0].xml + "<br>");
            document.write("stuName:" + nodes[2].childNodes[0].xml + "<br>");
            document.write("========<br/>");
            //node2.childNodes[0].childNodes[1].childNodes[0].text
            //取得是第几个stu-node2.childNodes[0]
            //取得第几个stu下面的节点
            //node2.childNodes[0].childNodes[0]
            //取得第几个stu下面的节点里面的值
            //node2.childNodes[0].childNodes[1].childNodes[0].text
        }
        function del() {
            var flag = confirm("是否确定删除?");
            if (flag) {
                var ok = "";
                var id = document.getElementById("sid").value;
                for (var i = 0; i < node2.childNodes.length; i++) {
                    if (node2.childNodes[i].attributes[0].text == id) {
                        ok = node2.childNodes[i];
                        node2.removeChild(ok);
                    }
                }
                alert(xmldoc.xml);
            }
        }
        function up() {
            var sid = document.getElementById("id").value;
            var name = document.getElementById("name").value;
            var age = document.getElementById("age").value;
            var sex = document.getElementById("sex").value;
            for (var i = 0; i < node2.childNodes.length; i++) {
                if (node2.childNodes[i].attributes[0].text == sid) {
                    var nn = node2.childNodes[i].childNodes;
                    nn[0].childNodes[0].text = name;
                    nn[1].childNodes[0].text = age;
                    nn[2].childNodes[0].text = sex;
                }
            }
            alert(xmldoc.xml);
        }
        function adds() {
            var sid = document.getElementById("id").value;
            var name = document.getElementById("name").value;
            var age = document.getElementById("age").value;
            var sex = document.getElementById("sex").value;
            //获取根节点
            var doc = xmldoc.getElementsByTagName("student")[0];
            //创建节点
            var stu = xmldoc.createElement("stu");
            var names = xmldoc.createElement("stuName");
            var ages = xmldoc.createElement("stuAge");
            var sexs = xmldoc.createElement("stuSex");
            var att = xmldoc.createAttribute("id"); //创建属性
            att.nodeValue = sid;
            //添加节点和值
            names.appendChild(xmldoc.createTextNode(name));
            ages.appendChild(xmldoc.createTextNode(age));
            sexs.appendChild(xmldoc.createTextNode(sex));
          stu.setAttributeNode(att);
            stu.appendChild(names);
            stu.appendChild(ages);
            stu.appendChild(sexs);
            doc.appendChild(stu);
            alert(xmldoc.xml);
        }
    </script>
</head>
<body>
请选择要删除的属性id
    <input id="sid" type="text" />
    <input id="button" type="button" value="删除"  />
<br/>
请选择要修改的属性id
    <input id="id" type="text" /><br/>
    姓名: <input id="name" type="text" /><br/>
    年龄: <input id="age" type="text" /><br/>
    性别: <input id="sex" type="text" /><br/>
      <input id="button1" type="button" value="修改"  />
      <input id="button2" type="button" value="添加"  />
      <br/>
</body>
</html>


你可能感兴趣的:(xml,dom)