访问VBA,XML和对象

时间:2014-04-15 20:19:19

标签: xml vba

我正在尝试解析XML文件并将Nodename和value加载到将作为集合返回的对象。当我尝试将Nodename添加到对象时,我得到“Object Required”错误。我试图将它直接添加到已定义的变量(通过set)以及创建变量然后将其添加到对象中,我得到相同的结果。

我在设置oXML.xElementAtt = txtElementName 行时遇到“需要对象”错误。感谢任何帮助。

Option Compare Database
Private xElementAtts As Variant
Private xElementValue As Variant
Private txtElementName As String
'-------------------------------------------------------------------------
'Class Name:        clsUtilities
'Class Function:    Basic utilities used by multiple classes
'Developer:         tsikora
'Last updated:      15-Apr-2014
'-------------------------------------------------------------------------
Public Function ParseXML(filename As String) As Collection
'Parses XML from Infopath Form

Dim xDoc As MSXML2.DOMDocument60
Set xDoc = New MSXML2.DOMDocument60
Dim xNode As MSXML2.IXMLDOMNode
Dim xmlNodeList As MSXML2.IXMLDOMNodeList
Dim myNode As MSXML2.IXMLDOMNode
Dim elm As MSXML2.IXMLDOMElement

Dim cXML As New Collection
Dim oXML As New clsUtilities
Dim sXMLNodeName As String

xDoc.async = False
xDoc.validateOnParse = False
If xDoc.Load(filename) Then
   ' The document loaded successfully.
   ' Now do something intersting.
    If xDoc.hasChildNodes Then
     Set xmlNodeList = xDoc.getElementsByTagName("*")
        For Each xNode In xmlNodeList
            'For Each myNode In xDoc.childNodes
                Debug.Print Space$(Indent) & xNode.nodeName & "-" & xNode.nodeTypeString & " " & _
                ":" & xNode.nodeTypedValue

                Select Case xNode.nodeType
                    Case NODE_ELEMENT
                        'do something
                        Set oXML = New clsUtilities
                        txtElementName = CStr(xNode.nodeName)
                        **Set oXML.xElementAtt = txtElementName**
                        Set oXML.xmlElementValue = xNode.nodeTypedValue
                        cXML.Add Item:=oXML

                    Case NODE_TEXT
    '                    Set oXML.xmlElementValue = xNode.nodeValue

                End Select
            'Next myNode
        Next xNode
    Else
    Exit Function
   ' The document failed to load.
   ' See the previous listing for error information.
End If

    Set ParseXML = cXML
End If
End Function


Property Get xElementAtt() As String
    xmlElementName = xElementAtts
End Property

Property Get xmlElementValue() As Variant
    xmlElementValue = xElementValue
End Property

Public Property Set xElementAtt(sElementName)
    xElementAtts = sElementName
End Property

Public Property Set xmlElementValue(sElementValue)
    xElementValue = sElementValue
End Property

0 个答案:

没有答案