我在网上搜索了大概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
答案 0 :(得分:1)
在有问题解析时始终验证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