当前位置: 首页 > 图文教程 > 网络编程 > Javascript > Javascript里使用Dom操作Xml

Javascript
javascript之循环停顿上下滚动
textarea保留换行的注意事项
不错的一篇关于javascript-prototype继承
非常不错的三种简洁的Tab导航(网页选项卡)简析
一个用js实现控制台控件的代码
【消息提示组件】,兼容IE6/7&&FF2
一个符号插入器 中用到的js代码
js中的escape及unescape函数的php实现代码
用javascript实现画板的代码
比较实用的复选框的实用javascript脚本
js下获得单选框的值的代码
IE不出现Flash激活框的小发现的js实现方法
超强的IE背景图片闪烁(抖动)的解决办法
“不能执行已释放的Script代码”错误的原因及解决办法
javascript下过滤数组重复值的代码
javascript在firefox下设为首页的代码
一直都需要的复制到系统剪贴板之IE,firefox兼容版
js类中获取外部函数名的方法与代码
JS类定义原型方法的两种实现的区别评论很多
一实用的实现table排序的Javascript类库

Javascript里使用Dom操作Xml


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 249 ::
收藏到网摘: n/a

一.Xml文件
二.IXMLDOMDocument/DOMDocument简介
2.1 属性
2.1.1 parseError
2.1.2 async.
2.1.3 xml
2.1.4 text 3
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();
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,返回“<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;