无法读取子节点使用XML DOM属性在Access 2003节点、属性、XML、DOM

2023-09-08 11:03:58 作者:Raccon浣熊

我已经在网上搜索或许8小时包括专家交流和#1,并发现其他有同样问题,但我还没有找到一个解决这个问题。

我正在附加其使用属性来存储数据的父节点和三个因子节点中的XML文件。所有属性都是唯一命名的。在所有浏览器的XML打开,我相当肯定,这是良好的。我已经成功地写入code读父节点,但每次我试图写code读取子节点的属性的属性,我得到一个运行时错误91或没有错误可言。

 <捐赠者>
    <捐赠DonorID =34224邮件=tsmith@gmail.comDonorFirstName =汤姆DonorMiddleName =DonorLastName =史密斯DonorAddress1 =2052主街DonorAddress2 =DonorCity =纽约DonorStateProv = 纽约DonorPostal code =10019DonorPhoneHome =2125298624DonorPhoneWork =DonorPhoneWorkExt =DonorPhoneMobile =DonorEmailAlternate =>
        < PageTypes>
              < PAGETYPE OnlinePageType =产品购买/>
        < / PageTypes>
        <交易>
             <交易的TransactionID =194CCTransactionID = -  999OrderTimeStamp =2013-05-24T07:16:37.333的OrderTotal =110.0000/>
        < /交易>
        <产品>
            <产品ProductGroupName =第一球门票产品名称=单产品描述=1票$ 10客户端code =I000001351ProductCount =1TotalFees =0.0000总金额=10.0000/>
            <产品ProductGroupName =第一球门票产品名称=12票产品描述=12票为$ 100客户端code =I000001352ProductCount =1TotalFees =0.0000总金额=100.0000/&GT ;
        < /产品>
< /供体GT;
 

我已经试过以下code MANY排列没有成功。我欢迎对如何周期的任何建议,通过这个XML,这样我可以处理和数据存储到两个相关的表中。

 功能ReadAttributes(BYVAL strXML作为字符串)

昏暗的xmlDoc中,作为新的DOMDocument
昏暗的iNode作为MSXML2.IXMLDOMNode
昏暗iNode2作为MSXML2.IXMLDOMNode
昏暗DonorNodeList作为的IXMLDOMNodeList
昏暗机构间工作组作为IXMLDOMAttribute
昏暗iAtt2作为IXMLDOMAttribute

对错误转到ReadAttributes_OnError

xmldoc.async =假
xmldoc.loadXML strXML
如果xmldoc.parseError.error code<> 0然后
    MSGBOX无效的XML,加载失败
    转到ReadAttributes_OnError
结束如果

设置DonorNodeList = xmldoc.getElementsByTagName(捐助)

对于每个inode在DonorNodeList
    对于每一个机构间工作组在iNode.Attributes
        MSGBOX iAtt.Name和放大器; :&安培; iAtt.nodeTypedValue
    下一个
    设置iNode2 = iNode.firstChild
    MSGBOX iNode2.nodeName
    对于每个iAtt2在iNode2.Attributes
        MSGBOX iAtt.Name和放大器; :&安培; iAtt.nodeTypedValue
    下一个
下一个
退出功能

ReadAttributes_OnError:
    MSGBOX的Err.Number和安培;  - &安培; Err.Description它将
    退出功能
端功能
 

解决方案

始终验证XML字符串时存在的问题分析它,

验证您的XML语法

复制你的字符串知道,如果它没有错误。

更新XML

 <捐赠者>
    <捐赠DonorID =34224邮件=tsmith@gmail.comDonorFirstName =汤姆DonorMiddleName =DonorLastName =史密斯DonorAddress1 =2052主街DonorAddress2 =DonorCity =纽约DonorStateProv = 纽约DonorPostal code =10019DonorPhoneHome =2125298624DonorPhoneWork =DonorPhoneWorkExt =DonorPhoneMobile =DonorEmailAlternate =>
        < PageTypes>
              < PAGETYPE OnlinePageType =产品购买/>
        < / PageTypes>
        <交易>
             <交易的TransactionID =194CCTransactionID = -  999OrderTimeStamp =2013-05-24T07:16:37.333的OrderTotal =110.0000/>
        < /交易>
        <产品>
            <产品ProductGroupName =第一球门票产品名称=单产品描述=1票$ 10客户端code =I000001351ProductCount =1TotalFees =0.0000总金额=10.0000/>
            <产品ProductGroupName =第一球门票产品名称=12票产品描述=12票为$ 100客户端code =I000001352ProductCount =1TotalFees =0.0000总金额=100.0000/&GT ;
        < /产品>
    < /供体GT;
< /捐助者>
 
xml取子节点的值 像解析JSON 那样简单的解析XML 文件 Python xmltodict 模块

 功能ReadAttributes(BYVAL strXML作为字符串)

    昏暗的xmlDoc中,作为新的DOMDocument
    昏暗的iNode作为MSXML2.IXMLDOMNode
    昏暗iNode2作为MSXML2.IXMLDOMNode
    昏暗DonorNodeList作为的IXMLDOMNodeList
    昏暗机构间工作组作为IXMLDOMAttribute
    昏暗iAtt2作为IXMLDOMAttribute

    对错误转到ReadAttributes_OnError

    xmldoc.async =假
    xmldoc.LoadXML strXML
    如果xmldoc.parseError.Error code<> 0然后
        MSGBOX无效的XML,加载失败
        转到ReadAttributes_OnError
    结束如果

    设置DonorNodeList = xmldoc.getElementsByTagName(捐助)

    对于每个inode在DonorNodeList
        对于每一个机构间工作组在iNode.Attributes
            MSGBOX iAtt.Name和放大器; :&安培; iAtt.nodeTypedValue
        下一个


        设置iNode2 = xmldoc.getElementsByTagName(捐助)(0)

        对于i = 0到iNode2.ChildNodes.Length  -  1
            对于每一个机构间工作组在iNode2.ChildNodes(我).ChildNodes(0).Attributes
                MSGBOX iAtt.Name和放大器; :&安培; iAtt.nodeTypedValue
            下一个
        下一个
    下一个


    退出功能

ReadAttributes_OnError:
    MSGBOX的Err.Number和安培;  - &安培; Err.Description它将
    退出功能
端功能
 

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.

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>

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

解决方案

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

Verify Your XML Syntax

Copy your string to know if its error free.

Updated 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