skip to Main Content

I am new to using XML with VBA and I am trying to parse XML responses form the eBay API such as this:

<?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>

I am using the following code to pull nodes out of the XML which seems to work for others but for me the oDoc.SelectSingleNode lines always return Null. I have tried removing the XML namespace manually and feeding that back into the objects, using XML v.4.0 objects, and have tried just about every combination of XPath I can think of. The VBA code I am using in its own module is:

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

The code to pull the info from the eBay servers works just fine the only issues I am having are trying to isolate the response values from the XML but I included it in case the issue lies in there somewhere. Ultimately I am just trying to get the value of a node such as ItemID = 232001428891.

Thanks in advance,

2

Answers


  1. Try xml linq. I used Load(filename) but you can use Parse(string)

    Imports System.Xml
    Imports System.Xml.Linq
    Module Module1
        Const FILENAME As String = "c:temptest.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
    
    Login or Signup to reply.
  2. Simple typo as the namespace declared in VBA does not align to XML, specifically you miss the apis:

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

    Change accordingly:

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

    Also, second .SelectSingleNode() does not point to existing path in posted XML, namely no <Title> element.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search