Javascript里使用Dom操作Xml一.本笔记使用的Xml文件二.IXMLDOMDocument/DOMDocument简介 2.1 属性2.1.1 parseError 2.1.2 async. 2.1.3 xml 2.1.4 text 32.1.5 attributes 2.1.6 nodeName2.1.7 documentElement 2.1.8 nextSibling2.1.9 childNodes2.1.10 firstChild2.1.11 lashChild2.2 方法2.2.1 loadXML2.2.2 load2.2.3 selectSingleNode2.2.4 selectNodes2.2.5 getElementsByTagName2.2.6 hasChildNodes三.例子一.本笔记使用的Xml文件
c++
20
1 2
在asp.net下实现代码:string str = Server.MapPath("test1.xml");
XmlTextWriter xmlWriter = new XmlTextWriter(str,null);
xmlWriter.Formatting = System.Xml.Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("book");
xmlWriter.WriteAttributeString("level","1");
xmlWriter.WriteElementString("Name","c++");
xmlWriter.WriteElementString("Price","20");
xmlWriter.WriteStartElement("info");
xmlWriter.WriteElementString("k","1");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("info");
xmlWriter.WriteElementString("k","2");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
二.IXMLDOMDocument/DOMDocument简介
2.1 属性
2.1.1 parseError
Returns an IXMLDOMParseError object that contains information about the last parsing error返回解析错误时的一个对象。重要的有parseError.errorCode,parseError.reason如果load时路径不对,会返回“系统未找到指定的对象”的错误2.1.2 async
Specifies whether asynchronous download is permitted是否允许异步下载,布尔值
2.1.3 xml
Contains the XML representation of the node and all its descendants. Read-only.该点及下面派生的所有点的全部信息,只读如果要求book点的xml,返回“c++2012”,如果Name的xml,返回“c++”2.1.4 text
Represents the text content of the node or the concatenated text representing the node and its descendants. Read/write该点及下面派生的所有点的全部节点值,可读可写20则text为20"Name"节点的text为"c++"2.1.5 attributes
Contains the list of attributes for this node返回属性的集合。2.1.6 nodeName
Returns the qualified name for attribute, document type, element, entity, or notation nodes. Returns a fixed string for all other node types. Read-only该节点名称"Name"节点的nodeName为"Name","book"节点的nodeName为"book"2.1.7 documentElement
Contains the root element of the documentxml的根节点上面的xml的根节点为"book"2.1.8 nextSibling
Contains the next sibling of the node in the parent's child list. Read-only.下一个兄弟节点,只读2.1.9 childNodes
Contains a node list containing the child nodes所有的子节点。2.1.10 firstChild
Contains the first child of the node第一个子节点2.1.11 lastChild
Returns the last child node最后一个子节点
2.2 方法
2.2.1 loadXML
Loads an XML document using the supplied string2.2.2 load
Loads an XML document from the specified locati参数的路径为服务器端的,是相对路径2.2.3 selectSingleNode
Applies the specified pattern-matching operation to this node's context and returns the first matching node返回第一个匹配的项2.2.4 selectNodes
Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes as IXMLDOMNodeList符合条件的所有项。2.2.5 getElementsByTagName
Returns a collection of elements that have the specified name返回与元素名匹配的一个node的集合2.2.6 hasChildNodes
Provides a fast way to determine whether a node has children判断是否含有子节点返回值为bool值
三.例子
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");xmlDoc.async = false;xmlDoc.load("test\\test1.xml");if (xmlDoc.parseError.errorCode!=0){var error = xmlDoc.parseError; alert(error.reason)return;}var root = xmlDoc.documentElement; //根节点Form1.test1.value = root.xml;/*结果如下:c++2012*/Form1.test1.value = root.nodeName; //结果为"book"var att = root.attributes; //得到该点下所有属性的集合var str = "";for (var i=0; i1”var fNode;var lNode;var nextSibling;fNode = root.firstChild; //第一个子节点NamelNode = root.lastChild; //最后一个子节点 infonextSibling = fNode.nextSibling; //第一个子节点Name的后一个兄弟节点,即Pricestr = fNode.nodeName + ":" + fNode.text; //结果:"Name:c++"str = lNode.nodeName + ":" + lNode.text; //结果为:"info:2"str = nextSibling.nodeName + ":" + nextSibling.text; //结果为:"Price:20"var nodeList;str = "";nodeList = xmlDoc.selectNodes("//info"); //查找元素名为"info"的节点for (var j=0; j2 ”}str = "";var sNode;sNode = xmlDoc.selectSingleNode("//info"); //找到第一个和"info"匹配的var scldNodes = sNode.childNodes; //info节点的子节点集for (var t=0; t1”Form1.test1.value = str;