很难纠正执行代码

我一直在试图从网站上提取一些XML数据,似乎无法让我的循环遍历每个应计date,最后一个元素应该是一个总费率而不是单日费率。 下面是代码,请温柔一点,因为我只是学习VBA和XML。 感谢您的帮助。

Sub getXMLFRN() Dim xmldoc As MSXML2.DOMDocument60 Dim xnodesls As MSXML2.IXMLDOMNodeList Dim xnode As MSXML2.IXMLDOMNode Dim strcusip As String Dim straccrualStart As String Dim straccrualEnd As String Dim strdailyAccruedInterestPer100 As String Dim strinterestPaymentPeriodAccruedInterestPer100 As String Set xmldoc = New MSXML2.DOMDocument60 xmldoc.async = False xmldoc.Load ("http://www.treasurydirect.gov/TA_WS/FRN/912828D31?format=xml") For i = 1 To 10 Application.Workbooks("xml test.xlsm").Worksheets("Sheet1").Range("A1:e1").Formula = Array("CUSIP", "Accrual Start", "Accrual End", "Daily Rate", "Total Rate") strcusip = xmldoc.getElementsByTagName("cusip").Item(0).Text straccrualStart = xmldoc.getElementsByTagName("accrualStart").Item(0).Text straccrualEnd = xmldoc.getElementsByTagName("accrualEnd").Item(0).Text strdailyAccruedInterestPer100 = xmldoc.getElementsByTagName("dailyAccruedInterestPer100").Item(0).Text strinterestPaymentPeriodAccruedInterestPer100 = xmldoc.getElementsByTagName("interestPaymentPeriodAccruedInterestPer100").Item(0).Text Sheets(1).Cells(i + 1, 1).Value = strcusip Sheets(1).Cells(i + 1, 2).Value = straccrualStart Sheets(1).Cells(i + 1, 3).Value = straccrualEnd Sheets(1).Cells(i + 1, 4).Value = strdailyAccruedInterestPer100 Sheets(1).Cells(i + 1, 5).Value = interestPaymentPeriodAccruedInterestPer100 Next i MsgBox ("Done") End Sub 

我无法让你的代码工作(我以前曾经遇到过这个问题),但是如果我更改为DOMDocumenttypes,我可以运行它。

我很惊讶这个代码工作,虽然也许DomDocument60支持一些其他function。 使用DOMDocument, .Load方法需要一个string(xml), 而不是一个URL。 当我运行你的代码时,我得到了这一行的错误。

无论如何,我使用XMLHttp从该URL获取响应文本,并将响应文本加载到xmldoc

问题是,当你有一个循环For i = 1 to 10 ,你不增加getElementsByTagName方法的索引位置 – 每次你执行该方法,你得到Item(0) 。 将其更改为Item(i-1) ,这应该起作用(假设至less有10个匹配元素(如果没有,则可能有错误)。

 Sub getXMLFRN() Dim xmlRequest As MSXML2.XMLHTTP Dim xmldoc As MSXML2.DOMDocument Dim xnodesls As MSXML2.IXMLDOMNodeList Dim xnode As MSXML2.IXMLDOMNode Dim strcusip As String Dim straccrualStart As String Dim straccrualEnd As String Dim strdailyAccruedInterestPer100 As String Dim strinterestPaymentPeriodAccruedInterestPer100 As String Set xmlRequest = New MSXML2.XMLHTTP xmlRequest.Open "Get", "http://www.treasurydirect.gov/TA_WS/FRN/912828D31?format=xml", False xmlRequest.Send Set xmldoc = New MSXML2.DOMDocument xmldoc.LoadXML xmlRequest.responseText For i = 1 To 10 Application.Workbooks("xml test.xlsm").Worksheets("Sheet1").Range("A1:e1").Formula = Array("CUSIP", "Accrual Start", "Accrual End", "Daily Rate", "Total Rate") strcusip = xmldoc.getElementsByTagName("cusip").Item(i-1).Text straccrualStart = xmldoc.getElementsByTagName("accrualStart").Item(i-1).Text straccrualEnd = xmldoc.getElementsByTagName("accrualEnd").Item(i-1).Text strdailyAccruedInterestPer100 = xmldoc.getElementsByTagName("dailyAccruedInterestPer100").Item(i-1).Text strinterestPaymentPeriodAccruedInterestPer100 = xmldoc.getElementsByTagName("interestPaymentPeriodAccruedInterestPer100").Item(i-1).Text Sheets(1).Cells(i + 1, 1).Value = strcusip Sheets(1).Cells(i + 1, 2).Value = straccrualStart Sheets(1).Cells(i + 1, 3).Value = straccrualEnd Sheets(1).Cells(i + 1, 4).Value = strdailyAccruedInterestPer100 Sheets(1).Cells(i + 1, 5).Value = interestPaymentPeriodAccruedInterestPer100 Next i MsgBox ("Done") End Sub 

更新

要处理未知/不同数量的项目,使用循环结构而不是使用硬编码的“10”或“100”等,使用文档的适当的最后索引。 这当然假设每个标签名称(“cusip”,“accrualstart”等)的数目是相同的,并且它们都是“相关的”(即,“cusip”.Item(0)涉及所有的另一个.Item(0)的,这似乎是真的)。

 For i = 1 To xmldoc.getElementsByTagName("cusip").length ''' ''' ''' Next i