一个解析XML的VB类

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

⼀个解析XML的VB类⾸先,安装控件msxml4.msi,并在VB中引⽤Microsoft XML。

类ClsXml.cls的程序如下:
Option Explicit
Private document As DOMDocument
Private action As IXMLDOMElement
Public Property Let xml(ByVal xml As String)
Set document = New DOMDocument
document.loadXML xml
'document.Load App.Path + "\from.xml"
Set action = document.selectSingleNode("action")
End Property
Function getNodeAttribute(ByVal attribute_name As String)
Dim element As IXMLDOMElement
Set element = action
If element Is Nothing Then
getNodeAttribute = ""
Else
getNodeAttribute = element.getAttribute(attribute_name)
End If
End Function
Function getNodeValue(ByVal node_name As String)
Dim node As IXMLDOMNode
Set node = action.selectSingleNode(node_name)
If node Is Nothing Then
getNodeValue = ""
Else
getNodeValue = node.Text
End If
End Function
Public Sub createDocument(ByVal name As String)
Set document = New DOMDocument
Set action = document.createElement("action")
action.setAttribute "name", name
document.appendChild action
End Sub
Public Sub appendNode(ByVal node_name As String, ByVal node_value As String)
Dim node As IXMLDOMNode
Set node = action.ownerDocument.createElement(node_name)
node.Text = node_value
action.appendChild node
End Sub
Public Property Get xml() As String
xml = document.xml
'document.save App.Path + "\to.xml"
End Property
调⽤程序如下:
Option Explicit
Private Sub Form_Load()
Dim obj As New ClsXml
obj.xml = "<action name=""login""><username>a</username><password>1</password></action>"
MsgBox obj.getNodeAttribute("name")
MsgBox obj.getNodeValue("username")
MsgBox obj.getNodeValue("password")
obj.createDocument "logout"
obj.appendNode "username", "a"
obj.appendNode "password", "1"
MsgBox obj.xml
Unload Me
End Sub。

相关文档
最新文档