返回首页
当前位置: 主页 > 编程语言 > XML教程 >

xml操作类

时间:2014-11-29 23:18来源:电脑教程学习网 www.etwiki.cn 编辑:admin

<%
Class XMLDOMDocument
Private fNode,fANode
Private fErrInfo,fFileName,fOpen
Dim XmlDom

'返回节点的缩进字串
Private Property Get TabStr(byVal Node)
TabStr=""
If Node Is Nothing Then Exit Property
If not Node.parentNode Is nothing Then TabStr=" "&TabStr(Node.parentNode)
End Property

'返回一个子节点对象,ElementOBJ为父节点,ChildNodeObj要查找的节点,IsAttributeNode指出是否为属性对象
Public Property Get ChildNode(byVal ElementOBJ,byVal ChildNodeObj,byVal IsAttributeNode)
Dim Element
Set ChildNode=Nothing

If IsNull(ChildNodeObj) Then
If IsAttributeNode=false Then
Set ChildNode=fNode
Else
Set ChildNode=fANode
End If
Exit Property
ElseIf IsObject(ChildNodeObj) Then
Set ChildNode=ChildNodeObj
Exit Property
End If

Set Element=Nothing
If LCase(TypeName(ChildNodeObj))="string" and Trim(ChildNodeObj)<>"" Then
If IsNull(ElementOBJ) Then
Set Element=fNode
ElseIf LCase(TypeName(ElementOBJ))="string" Then
If Trim(ElementOBJ)<>"" Then
Set Element=XmlDom.selectSingleNode("//"&Trim(ElementOBJ))
If Lcase(Element.nodeTypeString)="attribute" Then Set Element=Element.selectSingleNode("..")
End If
ElseIf IsObject(ElementOBJ) Then
Set Element=ElementOBJ
End If

If Element Is Nothing Then
Set ChildNode=XmlDom.selectSingleNode("//"&Trim(ChildNodeObj))
ElseIf IsAttributeNode=true Then
Set ChildNode=Element.selectSingleNode("./@"&Trim(ChildNodeObj))
Else
Set ChildNode=Element.selectSingleNode("./"&Trim(ChildNodeObj))
End If
End If
End Property

'读取最后的错误信息
Public Property Get ErrInfo
ErrInfo=fErrInfo
End Property

'给xml内容
Public Property Get xmlText(byVal ElementOBJ)
xmlText=""
If fopen=false Then Exit Property

Set ElementOBJ=ChildNode(XmlDom,ElementOBJ,false)
If ElementOBJ Is Nothing Then Set ElementOBJ=XmlDom

xmlText=ElementOBJ.xml
End Property

'=================================================================
'类初始化
Private Sub Class_Initialize()
Set XmlDom=CreateObject("Microsoft.XMLDOM")
XmlDom.preserveWhiteSpace=true

Set fNode=Nothing
Set fANode=Nothing

fErrInfo=""
fFileName=""
fopen=false
End Sub

'类释放
Private Sub Class_Terminate()
Set fNode=Nothing
Set fANode=Nothing
Set XmlDom=nothing
fopen=false
End Sub

'=====================================================================
'建立一个XML文件,RootElementName:根结点名。XSLURL:使用XSL样式地址
'返回根结点
Function Create(byVal RootElementName,byVal XslUrl)
Dim PINode,RootElement

Set Create=Nothing

If (XmlDom Is Nothing) Or (fopen=true) Then Exit Function

If Trim(RootElementName)="" Then RootElementName="Root"

Set PINode=XmlDom.CreateProcessingInstruction("xml", "version=""1.0"" encoding=""GB2312""")
XmlDom.appendChild PINode

Set PINode=XMLDOM.CreateProcessingInstruction("xml-stylesheet", "type=""text/xsl"" href="""&XslUrl&"""")
XmlDom.appendChild PINode

Set RootElement=XmlDom.createElement(Trim(RootElementName))
XmlDom.appendChild RootElement

Set Create=RootElement

fopen=True
set fNode=RootElement
End Function

'开打一个已经存在的XML文件,返回打开状态
Function Open(byVal xmlSourceFile)
Open=false

xmlSourceFile=Trim(xmlSourceFile)
If xmlSourceFile="" Then Exit Function

XmlDom.async = false
XmlDom.load xmlSourceFile

fFileName=xmlSourceFile

If not IsError Then
Open=true
fopen=true
End If
End Function

'关闭
Sub Close()
Set fNode=Nothing
Set fANode=Nothing

fErrInfo=""
fFileName=""
fopen=false
End Sub

'读取一个NodeOBJ的节点Text的值
'NodeOBJ可以是节点对象或节点名,为null就取当前默认fNode
Function getNodeText(byVal NodeOBJ)
getNodeText=""
If fopen=false Then Exit Function

Set NodeOBJ=ChildNode(null,NodeOBJ,false)
If NodeOBJ Is Nothing Then Exit Function

If Lcase(NodeOBJ.nodeTypeString)="element" Then
set fNode=NodeOBJ
Else
set fANode=NodeOBJ
End If

------分隔线----------------------------
标签(Tag):xml操作类
------分隔线----------------------------
推荐内容
猜你感兴趣