专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

一个解析XML的VB类

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:02:46       共计:3623 浏览
 

一个解析XMLVB

首先,安装控件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

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:VB6.0中Winsock控件是怎么使用 | ·下一条:VB6.0操作XML文件

Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有