我想在MS Access中使用VBA来读取某些XML,但我得到了一个' 对象变量或者没有设置块引用&# 39;解析此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
之后,我已经确定了以上代码为什么我收到错误的任何想法?
答案 0 :(得分:1)
目前,您的代码存在几个问题:
objXMLDoc
和XmlNamespaces
未声明。LoadXML()
用于xml字符串,而Load()
用于外部文件。ns2
。 IXMLDOMNodeList
对象但使用selectSingleNode()
方法,并另外使用列表索引作为文本值。这会引发类型不匹配错误。使用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
答案 1 :(得分: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")