【问题标题】:VBA XML parsing ''Object variable or With block reference not set'VBA XML解析“对象变量或未设置块引用”
【发布时间】:2016-02-23 08:59:31
【问题描述】:

我想在 MS Access 中使用 VBA 来读取某些 XML,但在解析此 XML 时出现“对象变量或未设置块引用”错误...

    <?xml version="1.0"?>
<GetCompetitivePricingForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
    <GetCompetitivePricingForASINResult ASIN="B002L7HJAA" status="Success">
        <Product
            xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"
            xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
            <Identifiers>
                <MarketplaceASIN>
                    <MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
                    <ASIN>B002L7HJAA</ASIN>
                </MarketplaceASIN>
            </Identifiers>
            <CompetitivePricing>
                <CompetitivePrices>
                    <CompetitivePrice belongsToRequester="false" condition="New" subcondition="New">
                        <CompetitivePriceId>1</CompetitivePriceId>
                        <Price>
                            <LandedPrice>
                                <CurrencyCode>GBP</CurrencyCode>
                                <Amount>14.45</Amount>
                            </LandedPrice>
                        </Price>
                    </CompetitivePrice>
                </CompetitivePrices>
            </CompetitivePricing>
        </Product>
    </GetCompetitivePricingForASINResult>
</GetCompetitivePricingForASINResponse>

使用此代码...

Public Function READXML()

Dim objXMLNode1 As MSXML2.IXMLDOMNodeList

Set objXMLDoc = New MSXML2.DOMDocument60

objXMLDoc.loadXML ("C:\Users\LW\Desktop\formatted.xml")

XmlNamespaces = "xmlns:ns2='http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd' xmlns:ns1='http://mws.amazonservices.com/schema/Products/2011-10-01'"

objXMLDoc.SetProperty "SelectionNamespaces", XmlNamespaces

Set objXMLNode1 = objXMLDoc.selectSingleNode("//ns2:Price/ns2:LandedPrice/ns2:Amount")

MsgBox objXMLNode1(0).text

End Function

如您所见,我试图提取 XML 值(在 XML 中显示为 14.45

我在谷歌上搜索了很长时间都无济于事,但是在阅读了'SelectSingleNode' XPath query from XML with multiple Namespaces之后,我选择了上面的代码

任何想法为什么我会收到错误?

【问题讨论】:

  • 哪一行出现错误? XmlNamespaces 是什么?它没有被宣布。尝试使用 Option Explicit 并在声明所有变量并修复所有拼写错误后编辑您的问题。
  • 您好 John,是 MsgBox objXMLNode1(0).text 行给出了错误。重新声明 XMLNameSpace ...好点!
  • @peskywinnets 您使用了错误的前缀。改用//ns1:Price/ns1:LandedPrice/ns1:Amount 试试...
  • 谢谢,但更改为 //ns1:Price/ns1:LandedPrice/ns1:Amount 仍然会导致相同的错误。

标签: xml vba parsing


【解决方案1】:

XPath 中提到的 XML 元素位于以下默认命名空间中:

xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"

除了其他答案中提供的一些好的建议之外,您应该使用 ns1 前缀,您已在 XPath 中声明引用默认命名空间:

XmlNamespaces = "xmlns:ns2='http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd' xmlns:ns1='http://mws.amazonservices.com/schema/Products/2011-10-01'"

objXMLDoc.SetProperty "SelectionNamespaces", XmlNamespaces

Set objXMLNode1 = objXMLDoc.selectSingleNode("//ns1:Price/ns1:LandedPrice/ns1:Amount")

【讨论】:

  • 谢谢 - 我尝试了你的建议,但它对我不起作用。
  • @peskywinnets - 根据我的最新评论,这确实有效,但您还必须清理列出的代码,如不匹配的对象引用。
【解决方案2】:

目前,您的代码存在几个问题:

  1. 未声明 objXMLDocXmlNamespaces
  2. LoadXML() 用于 xml 字符串,Load() 用于外部文件。
  3. 当标签当前没有这样的前缀时,您在 xpath 查询表达式中使用命名空间 ns2
  4. 您声明IXMLDOMNodeList 对象但使用selectSingleNode() 方法并另外使用文本值的列表索引。这会引发类型不匹配错误。
  5. 对于使用 MSXML v6.0 的未声明命名空间,您必须声明命名空间并在 xpath 表达式中引用它。

考虑下面使用NodeList()的调整代码:

Public Function READXML()
    Dim objxmldoc As New MSXML2.DOMDocument60
    Dim objXMLNode1 As MSXML2.IXMLDOMNodeList
    Dim xmlNamespaces As String

    objxmldoc.Load ("C:\Users\LW\Desktop\formatted.xml")
    xmlNamespaces = "xmlns:ns2='http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd' " _
                      & "xmlns:ns1='http://mws.amazonservices.com/schema/Products/2011-10-01'"

    objxmldoc.setProperty "SelectionNamespaces", xmlNamespaces
    Set objXMLNode1 = objxmldoc.SelectNodes("//ns1:Price/ns1:LandedPrice/ns1:Amount")

    MsgBox objXMLNode1(0).Text

    Set objxmldoc = Nothing

End Function

或者,使用SelectSingleNode() 方法:

Public Function READXML()
    Dim objxmldoc As New MSXML2.DOMDocument60
    Dim objXMLNode1 As MSXML2.IXMLDOMElement
    Dim xmlNamespaces As String

    objxmldoc.Load ("C:\Users\LW\Desktop\formatted.xml")
    xmlNamespaces = "xmlns:ns2='http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd' " _
                          & "xmlns:ns1='http://mws.amazonservices.com/schema/Products/2011-10-01'"

    objxmldoc.setProperty "SelectionNamespaces", xmlNamespaces
    Set objXMLNode1 = objxmldoc.SelectSingleNode("//ns1:Price/ns1:LandedPrice/ns1:Amount")        

    MsgBox objXMLNode1.Text

    Set objxmldoc = Nothing

End Function

【讨论】:

  • 你好 Parfait,我已经尝试了你的两个代码块,但我不得不改变你 Dim objxmldoc As New MSXML2.DOMDocument .....TO ....... Dim objxmldoc作为新的 MSXML2.DOMDocument60 (因为大概我使用的是 Microsoft XML ...V6.0 的更高版本)......但我仍然得到同样的错误
  • 只是为了澄清错误,您的建议在 Msgbox 行与 ''对象变量或未设置块引用'
  • 查看更新。对于 MSXML 6.0,您必须引用您声明为的未声明命名空间:ns1。我添加了#5 提到这一点。类似于@har07 提到的。
  • 谢谢 - 刚刚尝试了您编辑的代码(两个版本),但在 MsgBox 行仍然遇到相同的错误 :-(
  • 此代码适用于 Excel 2013。确保您的文件路径指向正确的位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-17
  • 2019-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多