打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
js xmlDom

1. 加载XML文档:

var xmlDom = new ActiveXObject("MSXML2.DOMDocument"); 
xmlDom.load("filename.xml"); //加载XML文件

2. 访问节点:

var root = xmlDom.documentElement;//获取根节点 
var nodeList = root.childNodes;  //获取节点的所有子节点 
var node = nodeList[i]; 
var name = node.attributes[0].value;//获取节点的第一个属性的值 
var xmlElement = node.xml;//包含起始标签+内容+结束标签 
var content = xmlElement.childNodes[0].xml;//若xmlElement不包括子节点,则可以获得xmlElement标签中的内容;若其包括子节点,则获得第一个子节点标签及其内容; 
var content = xmlElement.text;

3. 添加节点:

var newElement = xmlDom.createElement("element"); 
// 创建attribute属性,并添加到element节点上 
var attribute = xmlDom.createAttribute("attribute"); 
attribute.value = "attrubuteValue"; 
newElement.setAttributeNode(name); 
 
// 创建subElement子节点,并添加到newElement节点上 
var subElement = xmlDom.createElement("subElement"); 
newElement.text = "SubElementContent"; 
newElement.appendChild(subElement); 
//将newElement添加到根节点下 
root.appendChild(newElement); 

4. 删除节点:

var node = root.selectSingleNode("xpath"); 
if (node != null) 
     root.removeChild(node);

5. 保存节点:

xmlDom.save("driver:\\dir\filename.xml");//保存XML文件

6. Xpath几个例子:

authors 
authors/author 
authors/author/name 
authors/**//*/name 
authors/author/*            //*为通配符 
authors/author[nationality]/name      //用“[]”来限制只选取拥有nationality子节点的节点 
authors/author[nationality='Russian']/name //进一步限制子节点nationality的值为'Russian' 
authors/author[@period="classical"]    //选取属性period为"classical"的节点 
authors/author/@period         //选取节点的属性

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
VB.NET中操作xml文件
在C#.net中怎么操作XML
js 解析xml文件实现省市级联下拉菜单
C#操作XML方法集合
js 如何在一个元素节点之后插入元素
C#发现之旅第十讲 文档对象模型
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服