VB读取XML节点属性值

 XML文件内容如下:<?xml version="1.0" encoding="gb2312" ?> - <root> - <order name="订单数据" 测试使用="ceshisdfd">   <row 订单编号="62" 下单时间="2008-10-3 上午 12:28:30" 产品名称="金维" 产品价格="169" 当前状态="3" 广告编号="45" />   <row 订单编号="65" 下单时间="2008-10-4 上午 13:28:30" 产品名称="系列" 产品价格="166" 当前状态="5" 广告编号="41" />   <row 订单编号="63" 下单时间="2009-11-4 上午 11:08:57" 产品名称="系列" 产品价格="133" 当前状态="1" 广告编号="39" />   </order>   </root>

第1个回答  2011-11-11
我写了一个xml处理类,读读吧,参考下
'*********************************************************************************************
'AUTHOR Morn Woo
'完成时间 2011年4月18日
'版本 ver1.0 20110111,修正了部分错误
'*********************************************************************************************
Option Explicit
Option Compare Text
DefInt I
DefStr S
DefDate D
DefLng L
DefBool B

Dim mvarsXmlFile As String 'xmlFile属性内存变量
Dim mvarsXmlContent As String 'xmlContent属性内存变量
Dim mvarsXmlRoot As MSXML2.DOMDocument 'xmlRoot属性内存变量
Public Property Let XmlRoot(ByRef vData As MSXML2.DOMDocument)
Set mvarsXmlRoot = vData
End Property
Public Property Get XmlRoot() As MSXML2.DOMDocument
Set XmlRoot = mvarsXmlRoot
End Property

Public Property Let XmlFile(ByVal vData As String)
mvarsXmlFile = vData
End Property
Public Property Get XmlFile() As String
XmlFile = mvarsXmlFile
End Property

Public Property Let XmlContent(ByVal vData As String)
mvarsXmlContent = vData
End Property
Public Property Get XmlContent() As String
XmlContent = mvarsXmlContent
End Property

'类初始化
Private Sub Class_Initialize()

Me.XmlContent = ""
Me.XmlFile = ""
'Me.XmlRoot = New MSXML2.DOMDocument
Set mvarsXmlRoot = New MSXML2.DOMDocument
End Sub
'Private Sub Class_Terminate()
' XmlRoot.abort
'
'End Sub

Function fun_XmlLoad(ByVal sFilePath As String) As Boolean
On Error GoTo Morn
fun_XmlLoad = False
If Dir(sFilePath, vbNormal) = "" Then Exit Function

Me.XmlRoot.Load sFilePath
Me.XmlFile = Trim(sFilePath)
Do While XmlRoot.readyState <> 4
DoEvents
Loop
Me.XmlFile = XmlRoot.XML
fun_XmlLoad = True
Exit Function
Morn:

End Function
'找到节点对象
Function fun_GetElement(ByVal sItem As String) As MSXML2.IXMLDOMElement
'Set fun_GetElement = New ms

On Error GoTo Morn
Set fun_GetElement = Me.XmlRoot.selectSingleNode(sItem)
Exit Function
Morn:

End Function
'读取节点text函数
Function fun_GetElementText(ByVal sItem As String) As String
Dim oElement As MSXML2.IXMLDOMElement
fun_GetElementText = ""
On Error GoTo Morn
Set oElement = Me.XmlRoot.selectSingleNode(sItem)
If oElement Is Nothing Then Exit Function
fun_GetElementText = oElement.Text
Exit Function
Morn:

End Function
'读取节点属性值函数,默认读取value属性值
Function fun_GetProperty(ByVal sItem As String, Optional ByVal sPropertyName = "value") As String
Dim oElement As MSXML2.IXMLDOMElement
fun_GetProperty = ""
On Error GoTo Morn
Set oElement = Me.XmlRoot.selectSingleNode(sItem)
Set oElement = Me.XmlRoot.selectSingleNode(sItem)
If oElement Is Nothing Then Exit Function
fun_GetProperty = oElement.getAttribute(sPropertyName)
Exit Function
Morn:
End Function
相似回答