读写XML的问题.........................

我有一个XML文件,内容如下:

 1<record>
 2<elm_1>
 3<elm_3>null</elm_3>
 4</elm_1>
 5<elm_2>
 6<elm_4>null</elm_4>
 7<elm_5>
 8<elm_6>null</elm_6>
 9</elm_5>
10</elm_2>
11</record>

请问
(1)怎样在Elm_3和Elm_6处添加新节点、添加新节点数据?
(2)怎样删除任意一个终子节点,或包含子节点的任意非终节点?
(3)怎样修改任意一个节点的名称、属性、数据内容?
---------------------------------------------------------------

 1<script>   
 2x = new ActiveXObject("Msxml2.DOMDocument")   
 3x.loadXML("<Record><Elm_1><Elm_3>null</Elm_3></Elm_1><Elm_2><Elm_4>null</Elm_4><Elm_5><Elm_6>null</Elm_6></Elm_5></Elm_2></Record>")   
 4alert(x.xml)   
 5x3=x.selectSingleNode("/Record/Elm_1/Elm_3")   
 6  
 7x3new = x.createElement("Xa")   
 8x3new.text="333"   
 9x3.appendChild(x3new)   
10alert(x.xml)   
11  
12x6=x.selectSingleNode("//Elm_6")   
13  
14x6new = x.createElement("Xa")   
15x6new.text="666"   
16x6.appendChild(x6new)   
17alert(x.xml)   
18  
19xid=x.selectSingleNode("//Elm_1")   
20  
21xidnew = x.createAttribute("id")   
22xidnew.value="iddddddd"   
23  
24xid.attributes.setNamedItem(xidnew)   
25alert(x.xml)   
26  
27xid=x.selectSingleNode("//Elm_1/@id")   
28xidnew.value="idddddddnewnewnewnewnewnewnewnewnewnewnew"   
29alert(x.xml)   
30  
31xid=x.selectSingleNode("//Elm_4")   
32xid.text="新内容"   
33alert(x.xml)   
34  
35xid=x.selectSingleNode("//Elm_1")   
36xid.parentNode.removeChild(xid)   
37alert(x.xml)   
38  
39</script>
Published At
Categories with Web编程
comments powered by Disqus