Javascript里使用Dom操作Xml
看了一天的XML资料,感觉CSDN上这篇讲的挺细致的。即有Dot Net写入XML文件的示例,又有JS读取的示例,值得一看。(Source:http://blog.csdn.net/flypigluo)
一.本笔记使用的Xml文件
二.IXMLDOMDocument/DOMDocument简介
2.1 属性
2.1.1 parseError
2.1.2 async.
2.1.3 xml
2.1.4 text3
2.1.5 attributes
2.1.6 nodeName
2.1.7 documentElement
2.1.8 nextSibling
2.1.9 childNodes
2.1.10 firstChild
2.1.11 lashChild
2.2 方法
2.2.1 loadXML
2.2.2 load
2.2.3 selectSingleNode
2.2.4 selectNodes
2.2.5 getElementsByTagName
2.2.6 hasChildNodes
三.例子
一.本笔记使用的Xml文件
<?xml version="1.0"?>
<book level="1"> <Name>c++</Name>
<Price>20</Price>
<info>
<k>1</k>
</info>
<info>
<k>2</k>
</info>
</book>
在asp.net下实现代码:
string str = Server.MapPath("test1.xml");
XmlTextWriter xmlWriter = new XmlTextWriter(str,null);
xmlWriter.Formatting = System.Xml.Formatting.Indented;
xmlWriter.WriteStartDocument(); mlWriter.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,返回“<book level="1"><Name>c++</Name><Price>20</Price><info><k>1</k></info><info><k>2</k></info></book>”,如果Name的xml,返回“<Name>c++</Name>”
2.1.4 text
Represents the text content of the node or the concatenated text representing the node and its descendants. Read/write
该点及下面派生的所有点的全部节点值,可读可写
<price>20</price>
则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 document
xml的根节点
上面的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 string
2.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;
/*结果如下:
<book level="1"><Name>c++</Name><Price>20</Price><info><k>1</k></info><info><k>2</k></info></book>*/
Form1.test1.value = root.nodeName; //结果为"book"
var att = root.attributes; //得到该点下所有属性的集合
var str = "";
for (var i=0; i<att.length; i++)
{
str += att.item(i).nodeName+":"+att.item(i).text;
}
Form1.test1.value = str; //只有一个属性,所以结果为“level:1”
var fNode;
var lNode;
var nextSibling;
fNode = root.firstChild; //第一个子节点Name
lNode = root.lastChild; //最后一个子节点 info
nextSibling = fNode.nextSibling; //第一个子节点Name的后一个兄弟节点,即Price
str = 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; j<nodeList.length; j++) //有两个info节点
{
var infoNode = nodeList.item(j);
var cldNodes = infoNode.childNodes; //info节点的子节点集
for (var k=0; k<cldNodes.length; k++)
{
str += cldNodes.item(k).nodeName + ":" + cldNodes.item(k).text + " ";
}
//结果“k:1 k:2 ”
}
str = "";
var sNode;
sNode = xmlDoc.selectSingleNode("//info"); //找到第一个和"info"匹配的
var scldNodes = sNode.childNodes; //info节点的子节点集
for (var t=0; t<scldNodes.length; t++)
{
str += scldNodes.item(t).nodeName + ":" + scldNodes.item(t).text + " ";
}
//结果“k:1”
Form1.test1.value = str;