VB讀取xml處理類

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章