2

我在网上搜索了大约 8 个小时,包括 Experts Exchange 和 Stackoverflow,发现其他人也有同样的问题,但我还没有找到解决这个问题的方法。

我正在附加一个 XML 文件,该文件使用 Attributes 将数据存储在父节点和三个从属子节点中。所有的属性都是唯一命名的。XML 在所有浏览器中打开,我相当肯定它是“格式正确的”。我已经成功编写代码来读取父节点的属性,但是每次我尝试编写代码来读取子节点的属性时,我都会收到运行时错误 91 或根本没有错误。

<Donors>
    <Donor DonorID="34224" Email="tsmith@gmail.com" DonorFirstName="Tom" DonorMiddleName="" DonorLastName="Smith" DonorAddress1="2052 Main Street" DonorAddress2="" DonorCity="New York" DonorStateProv="New York" DonorPostalCode="10019" DonorPhoneHome="2125298624" DonorPhoneWork="" DonorPhoneWorkExt="" DonorPhoneMobile="" DonorEmailAlternate="">
        <PageTypes>
              <PageType OnlinePageType="Product Purchase" /> 
        </PageTypes>
        <Transaction>
             <Transactions TransactionID="194" CCTransactionID="-999" OrderTimeStamp="2013-05-24T07:16:37.333" OrderTotal="110.0000" /> 
        </Transaction>
        <Products>
            <Product ProductGroupName="First Pitch Tickets" ProductName="Single" ProductDescription="1 Ticket for $10" ClientCode="I000001351" ProductCount="1" TotalFees="0.0000" TotalAmount="10.0000" /> 
            <Product ProductGroupName="First Pitch Tickets" ProductName="12 Tickets" ProductDescription="12 tickets for $100" ClientCode="I000001352" ProductCount="1" TotalFees="0.0000" TotalAmount="100.0000" /> 
        </Products>
</Donor>

我已经尝试了以下代码的许多排列但没有成功。我欢迎任何有关如何循环浏览此 XML 的建议,以便我可以处理数据并将其存储到两个相关的表中。

Function ReadAttributes(ByVal strXML As String)

Dim xmldoc As New DOMDocument
Dim iNode As MSXML2.IXMLDOMNode
Dim iNode2 As MSXML2.IXMLDOMNode
Dim DonorNodeList As IXMLDOMNodeList
Dim iAtt As IXMLDOMAttribute
Dim iAtt2 As IXMLDOMAttribute

On Error GoTo ReadAttributes_OnError

xmldoc.async = False
xmldoc.loadXML strXML
If xmldoc.parseError.errorCode <> 0 Then
    MsgBox "Invalid XML, Load Failed"
    GoTo ReadAttributes_OnError
End If

Set DonorNodeList = xmldoc.getElementsByTagName("Donor")

For Each iNode In DonorNodeList
    For Each iAtt In iNode.Attributes
        MsgBox iAtt.Name & ": " & iAtt.nodeTypedValue
    Next
    Set iNode2 = iNode.firstChild
    MsgBox iNode2.nodeName
    For Each iAtt2 In iNode2.Attributes
        MsgBox iAtt.Name & ": " & iAtt.nodeTypedValue
    Next
Next
Exit Function

ReadAttributes_OnError:
    MsgBox Err.Number & " - " & Err.Description
    Exit Function
End Function
4

1 回答 1

1

解析时总是验证 xml 字符串,

验证您的 XML 语法

复制您的字符串以了解其是否没有错误。

更新的 XML

<Donors>
    <Donor DonorID="34224" Email="tsmith@gmail.com" DonorFirstName="Tom" DonorMiddleName="" DonorLastName="Smith" DonorAddress1="2052 Main Street" DonorAddress2="" DonorCity="New York" DonorStateProv="New York" DonorPostalCode="10019" DonorPhoneHome="2125298624" DonorPhoneWork="" DonorPhoneWorkExt="" DonorPhoneMobile="" DonorEmailAlternate="">
        <PageTypes>
              <PageType OnlinePageType="Product Purchase" /> 
        </PageTypes>
        <Transaction>
             <Transactions TransactionID="194" CCTransactionID="-999" OrderTimeStamp="2013-05-24T07:16:37.333" OrderTotal="110.0000" /> 
        </Transaction>
        <Products>
            <Product ProductGroupName="First Pitch Tickets" ProductName="Single" ProductDescription="1 Ticket for $10" ClientCode="I000001351" ProductCount="1" TotalFees="0.0000" TotalAmount="10.0000" /> 
            <Product ProductGroupName="First Pitch Tickets" ProductName="12 Tickets" ProductDescription="12 tickets for $100" ClientCode="I000001352" ProductCount="1" TotalFees="0.0000" TotalAmount="100.0000" /> 
        </Products>
    </Donor>
</Donors>

Function ReadAttributes(ByVal strXML As String)

    Dim xmldoc As New DOMDocument
    Dim iNode As MSXML2.IXMLDOMNode
    Dim iNode2 As MSXML2.IXMLDOMNode
    Dim DonorNodeList As IXMLDOMNodeList
    Dim iAtt As IXMLDOMAttribute
    Dim iAtt2 As IXMLDOMAttribute

    On Error GoTo ReadAttributes_OnError

    xmldoc.async = False
    xmldoc.LoadXML strXML
    If xmldoc.parseError.ErrorCode <> 0 Then
        MsgBox "Invalid XML, Load Failed"
        GoTo ReadAttributes_OnError
    End If

    Set DonorNodeList = xmldoc.getElementsByTagName("Donor")

    For Each iNode In DonorNodeList
        For Each iAtt In iNode.Attributes
            MsgBox iAtt.Name & ": " & iAtt.nodeTypedValue
        Next


        Set iNode2 = xmldoc.getElementsByTagName("Donor")(0)

        For i = 0 To iNode2.ChildNodes.Length - 1
            For Each iAtt In iNode2.ChildNodes(i).ChildNodes(0).Attributes
                MsgBox iAtt.Name & ": " & iAtt.nodeTypedValue
            Next
        Next
    Next


    Exit Function

ReadAttributes_OnError:
    MsgBox Err.Number & " - " & Err.Description
    Exit Function
End Function
于 2013-05-28T18:19:16.977 回答