2016-07-04 33 views
0

我是新來使用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

在此先感謝,

+0

XML文檔中的「eBay」名稱空間前綴在哪裏? – Parfait

+0

@Parfait糟糕對不起,我忘了把它包含在XML中。它現在在第二行。 –

回答

0

簡單的拼寫錯誤,如VBA聲明的命名空間不對齊XML,特別是你錯過了的API

<GetSingleItemResponse xmlns="urn:ebay:apis:eBLBaseComponents"> 
oDoc.SetProperty("SelectionNamespaces", "xmlns:eBay='urn:ebay:eBLBaseComponents'") 

相應地改變:

oDoc.SetProperty("SelectionNamespaces", "xmlns:eBay='urn:ebay:apis:eBLBaseComponents'") 

另外,第二.SelectSingleNode()未指向已發佈XML中的現有路徑,即沒有<Title>元素。

+0

謝謝。教會我複製別人的代碼而不仔細觀察.... –

-1

嘗試xml linq。我用負荷(文件名),但你可以使用解析(字符串)

Imports System.Xml 
Imports System.Xml.Linq 
Module Module1 
    Const FILENAME As String = "c:\temp\test.xml" 
    Sub Main() 
     Dim doc As XDocument = XDocument.Load(FILENAME) 

     Dim timestamp As DateTime = CType(doc.Descendants("Timestamp").FirstOrDefault(), DateTime) 
     Dim ack As String = CType(doc.Descendants("Ack").FirstOrDefault(), String) 
     Dim build As String = CType(doc.Descendants("Build").FirstOrDefault(), String) 
     Dim version As String = CType(doc.Descendants("Version").FirstOrDefault(), String) 

     Dim itemID As String = CType(doc.Descendants("ItemID").FirstOrDefault(), String) 
     Dim endTime As DateTime = CType(doc.Descendants("EndTime").FirstOrDefault(), DateTime) 
     Dim viewItemURLForNaturalSearch As String = CType(doc.Descendants("ViewItemURLForNaturalSearch").FirstOrDefault(), String) 
     Dim listingType As String = CType(doc.Descendants("ListingType").FirstOrDefault(), String) 
     Dim location As String = CType(doc.Descendants("Location").FirstOrDefault(), String) 

    End Sub 

End Module 
+0

我可能是無知的,但是這個VBA?對不起,如果我沒有在我的問題中說清楚。 –

+0

對不起,它是VBnet。 – jdweng