引用Excel VBA中的特定节点时出错

时间:2015-11-24 19:24:47

标签: excel-vba nodes vba excel

我正在尝试将汇率导入我的工作表,但是我无法从加拿大银行的RSS Feed中解析xml

所以我的一些XML文档看起来像这样

<item rdf:about="http://www.bankofcanada.ca/stats/assets/rates_rss/noon/en_SEK_NOON.xml">
    <title xml:lang="en">CA: 6.5402 SEK_NOON = 1 CAD 2015-11-24 Bank of Canada noon rate</title>
    <link>http://www.bankofcanada.ca/?page_id=39919</link>
    <description xml:lang="en">1 Canadian Dollar = 6.5402 SEK_NOON (SEK_NOON = Swedish krona). These are the Bank of Canada's nominal noon exchange rates, published at about 12:30 ET each business day. These are neither buying nor selling rates, and should be used for reference purposes only.</description>
    <dc:date>2015-11-24T12:30:00-05:00</dc:date>
    <dc:language>en</dc:language>
    <cb:statistics>
        <cb:country>CA</cb:country>
        <cb:exchangeRate>
            <cb:value decimals="2">6.5402</cb:value>
            <cb:baseCurrency>CAD</cb:baseCurrency>
            <cb:targetCurrency>SEK_NOON</cb:targetCurrency>
            <cb:rateType>Bank of Canada noon rate</cb:rateType>
            <cb:observationPeriod frequency="daily">2015-11-24T12:30:00-05:00</cb:observationPeriod>
        </cb:exchangeRate>
    </cb:statistics>
</item>
<item rdf:about="http://www.bankofcanada.ca/stats/assets/rates_rss/noon/en_CHF_NOON.xml">
    <title xml:lang="en">CA: 0.7635 CHF_NOON = 1 CAD 2015-11-24 Bank of Canada noon rate</title>
    <link>http://www.bankofcanada.ca/?page_id=39919</link>
    <description xml:lang="en">1 Canadian Dollar = 0.7635 CHF_NOON (CHF_NOON = Swiss franc). These are the Bank of Canada's nominal noon exchange rates, published at about 12:30 ET each business day. These are neither buying nor selling rates, and should be used for reference purposes only.</description>
    <dc:date>2015-11-24T12:30:00-05:00</dc:date>
    <dc:language>en</dc:language>
    <cb:statistics>
        <cb:country>CA</cb:country>
        <cb:exchangeRate>
            <cb:value decimals="4">0.7635</cb:value>
            <cb:baseCurrency>CAD</cb:baseCurrency>
            <cb:targetCurrency>CHF_NOON</cb:targetCurrency>
            <cb:rateType>Bank of Canada noon rate</cb:rateType>
            <cb:observationPeriod frequency="daily">2015-11-24T12:30:00-05:00</cb:observationPeriod>
        </cb:exchangeRate>
    </cb:statistics>
</item>

我试图提取交换价值。使用我的代码我能够提取节点exchangeRate,我想从该父节点获取value和targetCurrency节点。但是我似乎很难得到它们

 Sub ParseXML()
 Dim feedresult As String

 Dim xDoc As MSXML2.DOMDocument60
     feedresult = GetHTTPResponse("http://www.bankofcanada.ca/stats/assets/rates_rss/noon/en_all.xml")
Set xDoc = New MSXML2.DOMDocument60
xDoc.LoadXML (feedresult)

Dim currList As IXMLDOMNodeList
Set currList = xDoc.getElementsByTagName("cb:exchangeRate")

Dim currNode As IXMLDOMNode
Dim childNode As IXMLDOMNode

For Each currNode In currList
    exchangeRate = currNode.SelectSingleNode("value").Text
    Debug.Print exchangeRate
Next currNode
End Sub

但是我经常得到一个Run Time Error 91 Object变量或With block not set

我知道内部节点的基本名称是值,因为我已经通过exchangeRate的子节点的baseName进行了交互。如果我使用以下代码,那么我实现了我想要的

For Each childNode In currNode.ChildNodes
        If childNode.BaseName = "targetCurrency" Then
            tc = Left(childNode.Text, 3)
        End If
        If childNode.BaseName = "value" Then
            exchangeRate = childNode.Text
        End If
Next childNode

但是我意识到这是一个非常不优雅的解决方案,我应该能够通过它们的基本名称引用子节点。

提前感谢您的帮助。

0 个答案:

没有答案