XMLDOM 简明教程

作者:vkvi 来源:ITPOW(原创) 日期:2006-9-28

XMLDOM 是用于访问 XML 文档的组件,IIS 中默认是安装了的。

相关阅读XMLHTTP 速查 IE 数据岛(1) IE 数据岛(2) IE 数据岛(3) IE 数据岛(4)

示例
dim xmlDoc
set xmlDoc = server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = false
xmlDoc.Load(server.MapPath("xmldata.xml"))

if xmlDoc.parseError.errorCode <> 0 then
    '发生错误
    response.Write("错误说明:" & xmlDoc.parseError.reason)
else
    '显示数据
    dim i
    for i=0 to xmlDoc.childNodes(1).childNodes.length-1
        response.Write("<p>" & xmlDoc.childNodes(1).childNodes(i).childNodes(0).childNodes(0).nodeValue & "</p>")
    next
end if

set xmlDoc = nothing

讲解
async true-异步操作,false-同步操作,如果是同步操作则在读取完毕后再执行其它操作,所以一般为 false。
Load(xmlFilePath) 读取本地磁盘的 XML 文件。
LoadXML(xml) 读取 XML 文本,比如:<?xml version="1.0" ?><root></root>。
parseError 错误信息对象,常用的属性是 errorCode 和 reason。
errorCode 错误代码,0 表示没有发生错误。
reason 错误的文字说明。
childNodes XML 子节点,子节点还可以包含子节点,数组形式,所以可以用 length 表示当前节点的子节点个数。第一个 xmlDoc.childNodes(0) 是 <?xml version="1.0" encoding="gb2312"?>,xmlDoc.childNodes(1) 才是 XML “正文”。
nodeType 节点类型
nodeTypeString 节点类型文字描述
nodeName 节点名称
nodeValue 节点值

nodeType nodeTypeString nodeName nodeValue
1 element tagName null
2 attribute name value
3 text #text content of node
4 cdatasection #cdata-section content of node
5 entityreference entity reference name null
6 entity entity name null
7 processinginstruction target content of node
8 comment #comment comment text
9 document #document null
10 documenttype doctype name null
11 documentfragment #document fragment null
12 notation notation name null

示例
dim xmlDoc, xslDoc
set xmlDoc = server.CreateObject("Microsoft.XMLDOM")
set xslDoc = server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = false
xslDoc.async = false

xmlDoc.Load(server.MapPath("xmlData.xml"))
xslDoc.Load(server.MapPath("xslData.xsl"))
response.Write(xmlDoc.TransformNode(xslDoc))

set xslDoc = nothing
set xmlDoc = nothing

讲解
本例中使用两个 XMLDOM 对象,用 XSL 文件将 XML 文件格式化为 HTML 文件输出。
xmlDoc.TransformNode(xslDoc)) 返回 HTML 文档。

其它

Property Description
nodeType 节点类型
nodeTypeString 节点类型文字描述
nodeName 节点名称
nodeValue 节点值
attributes 当前节点的所有属性对象
childNodes 当前节点的所有子节点对象
parentNode 当前节点的父节点
ownerDocument 文档的根节点
FirstChild 当前节点的第一个子节点
LastChild 当前节点的最后一个子节点
PreviousSibling 当前节点的上一个兄弟节点
BextSibling 当前节点的下一个兄弟节点
HasChildNodes() 是否有子节点
CloneNode(allChilds) 复制并返回节点,allChilds 为 true,则复制所有的子节点
InsertBefore(newNode, refNode) 在节点-refNode 的前面插入新的节点
AppendChild(newChild) 向当前节点追加一个子节点
ReplaceChild(newNode, oldNode) 用新节点替换原节点
RemoveChild(nodeName) 删除子节点
async true-异步,false-同步
Load 读取本地 XML 文档
LoadXML 读取 XML 文本。
parseError 错误对象
TransformNode 用 XSL 文件将 XML 文件格式化为 HTML 文件并返回

相关阅读

相关文章