我是新來使用XML與VBA,我試圖解析XML響應形成的eBay API像這樣:使用DOMDocument.SelectSingleNode隨着XML命名空間始終返回NULL VBA
<?xml version="1.0"?>
<GetSingleItemResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2016-07-04T06:24:28.969Z</Timestamp>
<Ack>Success</Ack>
<Build>E963_CORE_APILW_17911290_R1</Build>
<Version>963</Version>
<Item>
<ItemID>232001428891</ItemID>
<EndTime>2016-07-13T22:06:14.000Z</EndTime>
<ViewItemURLForNaturalSearch>http://www.ebay.com/itm/WW2-Australian-P37-Entrenching-Tool-Cover-/232001428891</ViewItemURLForNaturalSearch>
<ListingType>Chinese</ListingType>
<Location>Pambula, New South Wales</Location>
</Item>
</GetSingleItemResponse>
我使用以下代碼拉出節點的XML似乎爲其他人工作,但對於我oDoc.SelectSingleNode
行總是返回Null
。我曾嘗試手動刪除XML名稱空間,並使用XML v.4.0對象將其反饋回對象,並嘗試了幾乎所有我能想到的XPath組合。我使用自己的模塊中的VBA代碼:
Public Sub GeteBayItem()
'create the xml string
Dim itemXML As String
'Populate itemXML String with necessary values
'.................
'the http connection
Dim httpCnct As XMLHTTP60
Set httpCnct = New XMLHTTP60
'using POST synchronous call
httpCnct.Open "POST", "http://open.api.ebay.com/shopping?", False
'set the headers
'.........
Dim xmlDoc As FreeThreadedDOMDocument60
Set xmlDoc = New FreeThreadedDOMDocument60
xmlDoc.async = False
xmlDoc.LoadXML itemXML
'Make the call
httpCnct.send xmlDoc
Dim oNode As IXMLDOMNode
Dim oNode1 As IXMLDOMNode
Dim oDoc As DOMDocument60
Set oDoc = httpCnct.responseXML
Call oDoc.SetProperty("SelectionNamespaces", "xmlns:eBay='urn:ebay:eBLBaseComponents'")
'Debug.Print oDoc.XML
'Declaring a XML Doc
'Get the Item Number
Set oNode = oDoc.SelectSingleNode("/eBay:GetSingleItemResponse/eBay:Item/eBay:ItemID")
'Get the title
Set oNode1 = oDoc.SelectSingleNode("/eBay:GetSingleItemResponse/eBay:Item/eBay:Title")
'Set oNode = oDoc.SelectSingleNode(「/GetItemResponse」)
MsgBox (oNode.Text)
MsgBox (oNode1.Text)
Set oNode = Nothing
Set oNode1 = Nothing
Set oDoc = Nothing
End Sub
從eBay的服務器拉信息代碼工作得很好,我有嘗試的響應值從XML隔離的唯一問題,但我包括在案件中,以防問題出現在某處。最終,我只是想獲得節點的價值,如ItemID = 232001428891
。
在此先感謝,
XML文檔中的「eBay」名稱空間前綴在哪裏? – Parfait
@Parfait糟糕對不起,我忘了把它包含在XML中。它現在在第二行。 –