700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > XML DOM学习笔记(JS)

XML DOM学习笔记(JS)

时间:2020-02-23 20:20:28

相关推荐

XML DOM学习笔记(JS)

1. 加载XML文档:

varxmlDom=newActiveXObject("MSXML2.DOMDocument");

xmlDom.load("filename.xml");//加载XML文件

2. 访问节点:

varroot=xmlDom.documentElement;//获取根节点

varnodeList=root.childNodes;//获取节点的所有子节点

varnode=nodeList[i];

varname=node.attributes[0].value;//获取节点的第一个属性的值

varxmlElement=node.xml;//包含起始标签+内容+结束标签

varcontent=xmlElement.childNodes[0].xml;//若xmlElement不包括子节点,则可以获得xmlElement标签中的内容;若其包括子节点,则获得第一个子节点标签及其内容;

3. 添加节点:

varnewElement=xmlDom.createElement("element");

//创建attribute属性,并添加到element节点上

varattribute=xmlDom.createAttribute("attribute");

attribute.value="attrubuteValue";

newElement.setAttributeNode(name);

//创建subElement子节点,并添加到newElement节点上

varsubElement=xmlDom.createElement("subElement");

newElement.text="SubElementContent";

newElement.appendChild(subElement);

//将newElement添加到根节点下

root.appendChild(newElement);

4. 删除节点:

varnode=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//选取节点的属性

7. 介绍Xpath的两个网址:

/xxl/XPathTutorial/General_chi/examples.html

/xpath/index.asp

8. 一个解析Xpath的工具:

该工具的下载地址:/nleghari/visualxpath.zip

本文转自Silent Void博客园博客,原文链接:/happyhippy/archive//07/24/829001.html,如需转载请自行联系原作者

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。