日期:2014-05-16 浏览次数:20307 次
01.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">?
02.<html xmlns="http://www.w3.org/1999/xhtml" >?
03.<head>?
04.??? <title>js操作Xml增删改查(IE下)</title>?
05.??? <mce:script type="text/javascript"><!--??
06.??? /*等解决的问题:??
07.????? 1.xpath到底是定位到哪一层,怎样定位到比如root这一级还是person或name这一级.??
08.??? */??
09.??? var xmlDoc;??
10.??? var rootNode; //根结点??
11.??? //装载Xml文档??
12.??? function loadXml(){??
13.??????? try{??
14.??????????? xmlDoc = new ActiveXObject("Microsoft.XMLDOM");??
15.??????????? xmlDoc.async=false;//关闭异步加载??
16.??????????? xmlDoc.load("XmlFile.xml");//load是从文件,loadXML是从字符串.??
17.??????????? rootNode = xmlDoc.documentElement;??
18.??????? }catch(e) {alert(e.message)}??
19.??? }??
20.??? //显示内存中的Xml文档??
21.??? function outXml(){??
22.??????? var divXml=document.getElementById("divXml");??
23.??????? divXml.innerHTML=xmlDoc.xml;//显示xml内容,技巧是加个xml后缀.???
24.??????? alert(xmlDoc.xml);??
25.??? }??
26.??? //增??
27.??? function addXml(){??
28.??????? //叶子结点,设置text值??
29.??????? var newName = xmlDoc.createElement("name");???
30.??????? newName.text = "crane";??
31.??????? var newGender = xmlDoc.createElement("gender");???
32.??????? newGender.text = "female";??
33.??????? //父级结点,用appendChild(childNode);??
34.??????? var newPerson = xmlDoc.createElement("person");???
35.??????? //设置属性id??
36.??????? newPerson.setAttribute("id","2");??
37.??????? newPerson.appendChild(newName);??
38.??????? newPerson.appendChild(newGender);??
39.??????? //增加到根结点??
40.??????? rootNode.appendChild(newPerson);??
41.??????? alert(xmlDoc.xml);??
42.??? }??
43.??? //删??
44.??? function deleteXml(){??
45.??????? //先找到结点??
46.??????? var singleNode = xmlDoc.selectSingleNode("/root/person[name='tree']");??
47.??????? //找到父级再删除??
48.??????? singleNode.parentNode.removeChild(singleNode);???
49.??????? alert(xmlDoc.xml);??
50.??? }??
51.??? //改??
52.??? function updateXml(){??
53.??????? var singleNode = xmlDoc.selectSingleNode("/root/person[name='crane']");??
54.??????? singleNode.childNodes[0].text = "updated";??
55.??????? alert(xmlDoc.xml);??
56.??? }??
57.??? //查??
58.??? function queryXml(){??
59.??????? //alert(rootNode.nodeName);//节点名??
60.??????? //alert(rootNode.text);//节点里的全部内容??
61.??????? //xPath选择节点数组??
62.??????? //var nodes = xmlDoc.selectNodes("/root/person");??
63.??????? //alert(nodes[0].text);??
64.??????? //选择单个节点??
65.??????? /*总结??
66.????????? 1."/root/person[name='tree']"等同于"/root[person/name='tree']"即找出来的是person结点??
67.??????? */??
68.??????? var singleNode = xmlDoc.selectSingleNode("/root/person[gender='female']");//这里的值需要加引号??
69.??????? alert(singleNode.text);??
70.??????? alert(singleNode.getAttribute("id"));??
71.??????? //测试xpath定位??
72.??????? var sglNode = xmlDoc.selectSingleNode("/root[person/gender='male']");//这里定位不明确.再研究.??
73.??????? alert(sglNode.text);??
74.??????? //显示全部xml文档??
75.??????? //alert(xmlDoc.xml);??
76.??? }??
77.??????
78.// --></mce:script>?
79.</head>?
80.<body>?
81.<div id="divXml"></div>?
82.<input type="button" value="load" onclick="loadXml();" />?
83.<input type="button" value="show" onclick="outXml();" />?
84.<input type="button" value="add" onclick="addXml();" />?
85.<input type="button" value="delete" onclick="deleteXml();" />?
86.<input type="button" value="update" onclick="updateXml();" />?
87.<input type="button" value="query" onclick="queryXml();" />?
88.</body>?
89.</html>?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.