您的当前位置:我要购书网>在线阅读>ASP/PHP/.NET/XML

javascript里使用dom操作xml-asp.net入门笔记(十一)

来源:互联网  作者:  发布:我要购物网收集整理  发布时间:2006-8-31 人气:455

Javascript里使用Dom操作Xml一.本笔记使用的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 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;

相关文章
· javascript里使用dom操作xml-asp.ne..
· javax.servlet.requestdispatc..
· java性能优化技巧集锦
· java代码编写的30条建议
· java读取excel方式对比
· javax.servlet.servletcontext..
· javascript高级教程 - 第一课
· javascript 中的replace方法
· javascript高级教程- 第5天
· java.lang.noclassdeffounderr..
热点文章
%>
· creating user controls
· 提高fastreplace速度 (fstrrep.pas)
· asc ii 完整码表及简介
· 自动生成拼音(汉字反查到拼音)
· sql2000无法安装的解决办法
· 浏览器集成教学 自定义浏览器
· vc++技术内幕(第四版)笔记(第7章)
· mysql5.0中文乱码解决方案
· sql server日期计算
· vc下利用ado连接access数据库
 淘宝旺旺:我要购书网上书店『图书目录
本购书中心地址: 杭州市延安路111号清波商厦南楼D座(总部) 上海市闸北区老沪太路网上购书中心(沪部),  邮编:310002
电子邮件:books@51goushu.com  经营许可证编号:沪ICP备06038574号
版权所有 2003-2008 © All Rights Reserved .购书网