当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript Xml增删改查(IE下)操作实现代码

Javascript
JavaScript教程:浅析JS运行机制
JavaScript类型检测小结
JavaScript内置对象:arguments
编写复杂环境下不出错的Javascript代码
掌握JavaScript语言
IE7中网页动态创建iframe时边框问题
总结常用JavaScript语法107条
AJAX教程(1):AJAX简介
AJAX教程(2):AJAX Http请求
AJAX教程(3):AJAX实例
AJAX教程(4):AJAX 浏览器支持
AJAX教程(5):AJAX-XMLHttpRequest 对象
AJAX教程(6):AJAX - 请求服务器
AJAX教程(7):AJAX - 服务器端的脚本
AJAX教程(8):AJAX 请求实例
AJAX教程(9):AJAX 请求 源代码
AJAX教程(10):AJAX 数据库实例
AJAX教程(11):AJAX XML 实例
AJAX教程(12):AJAX ResponseXML 实例
AJAX教程(13):通过XMLHTTP把文本文件载入HTML元素

Javascript 中的 javascript Xml增删改查(IE下)操作实现代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 478 ::
收藏到网摘: n/a

比较不错的实现代码,大家可以仔细的看下,思路。 html文件:
复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>js操作Xml增删改查(IE下)</title>
<script type="text/javascript"><!--
/*等解决的问题:
1.xpath到底是定位到哪一层,怎样定位到比如root这一级还是person或name这一级.
*/
var xmlDoc;
var rootNode; //根结点
//装载Xml文档
function loadXml(){
try{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;//关闭异步加载
xmlDoc.load("XmlFile.xml");//load是从文件,loadXML是从字符串.
rootNode = xmlDoc.documentElement;
}catch(e) {alert(e.message)}
}
//显示内存中的Xml文档
function outXml(){
var divXml=document.getElementById("divXml");
divXml.innerHTML=xmlDoc.xml;//显示xml内容,技巧是加个xml后缀.?
alert(xmlDoc.xml);
}
//增
function addXml(){
//叶子结点,设置text值
var newName = xmlDoc.createElement("name");
newName.text = "crane";
var newGender = xmlDoc.createElement("gender");
newGender.text = "female";
//父级结点,用appendChild(childNode);
var newPerson = xmlDoc.createElement("person");
//设置属性id
newPerson.setAttribute("id","2");
newPerson.appendChild(newName);
newPerson.appendChild(newGender);
//增加到根结点
rootNode.appendChild(newPerson);
alert(xmlDoc.xml);
}
//删
function deleteXml(){
//先找到结点
var singleNode = xmlDoc.selectSingleNode("/root/person[name='tree']");
//找到父级再删除
singleNode.parentNode.removeChild(singleNode);
alert(xmlDoc.xml);
}
//改
function updateXml(){
var singleNode = xmlDoc.selectSingleNode("/root/person[name='crane']");
singleNode.childNodes[0].text = "updated";
alert(xmlDoc.xml);
}
//查
function queryXml(){
//alert(rootNode.nodeName);//节点名
//alert(rootNode.text);//节点里的全部内容
//xPath选择节点数组
//var nodes = xmlDoc.selectNodes("/root/person");
//alert(nodes[0].text);
//选择单个节点
/*总结
1."/root/person[name='tree']"等同于"/root[person/name='tree']"即找出来的是person结点
*/
var singleNode = xmlDoc.selectSingleNode("/root/person[gender='female']");//这里的值需要加引号
alert(singleNode.text);
alert(singleNode.getAttribute("id"));
//测试xpath定位
var sglNode = xmlDoc.selectSingleNode("/root[person/gender='male']");//这里定位不明确.再研究.
alert(sglNode.text);
//显示全部xml文档
//alert(xmlDoc.xml);
}
// --></script>
</head>
<body>
<div id="divXml"></div>
<input type="button" value="load" onclick="loadXml();" />
<input type="button" value="show" onclick="outXml();" />
<input type="button" value="add" onclick="addXml();" />
<input type="button" value="delete" onclick="deleteXml();" />
<input type="button" value="update" onclick="updateXml();" />
<input type="button" value="query" onclick="queryXml();" />
</body>
</html>

Xml文件:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<root>
<person id="1">
<name>tree</name>
<gender>male</gender>
</person>
</root>