无法在 Access 2003 中使用 XML DOM 读取子节点的属性 [英] Unable to read the attributes of child nodes using XML DOM in Access 2003

查看:11
本文介绍了无法在 Access 2003 中使用 XML DOM 读取子节点的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have searched the web for perhaps 8 hours including Experts Exchange and Stackoverflow and have found others with the same issue but I've not found a solution to this issue.

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

I'm attaching an XML file which uses Attributes to store the data in a parent node and three dependent child nodes. All of the Attributes are uniquely named. the XML opens in all browsers and I'm fairly certain that it is "well-formed". I have successfully written code to read the attributes of the parent node but every time I attempt to write code to read the attributes of the child nodes, I get a run time error 91 or no error at all.

<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 的建议,以便我可以处理数据并将其存储到两个相关的表中.

I've tried MANY permutations of the following code without success. I welcome any suggestions on how to cycle through this XML so that I can process and store the data into two related tables.

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

推荐答案

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

Always validate xml string when there is issue to parse it,

验证您的 XML 语法

复制您的字符串以了解其是否正确.

Copy your string to know if its error free.

更新的 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

这篇关于无法在 Access 2003 中使用 XML DOM 读取子节点的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆