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

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 326 ::
收藏到网摘: 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>