使用DOMDocument.SelectSingleNode与XML命名空间始终返回空VBA

时间:2016-07-04 21:00:04

标签: xml vba xml-parsing

我是使用XML和VBA的新手,我正在尝试从eBay API解析XML响应,例如:

<?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等节点的价值。

提前致谢,

2 个答案:

答案 0 :(得分:0)

简单的拼写错误,因为在VBA中声明的命名空间与XML不对齐,特别是您错过了 apis

<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>元素。

答案 1 :(得分:-1)

试试xml linq。我使用了Load(filename),但你可以使用Parse(string)

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