SOAP响应的命名空间不能与XPath一起使用

时间:2018-04-09 02:39:23

标签: xml vbscript namespaces

我有一条SOAP消息如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header xmlns:v1="http://us.com/InventoryService/v1/"/>
<soapenv:Body xmlns:v1="http://us.com/InventoryService/v1/">
    <v1:InventoryServiceResponse>
        <v1:InventoryInfo>
            <v1:productIdVal>ToothPick</v1:productIdVal>
            <v1:localQuantityVal>0</v1:localQuantityVal>
            <v1:localPlusQuantityVal>44</v1:localPlusQuantityVal>
            <v1:nationalQuantityVal>1475</v1:nationalQuantityVal>
            <v1:customerInventoryVal>0</v1:customerInventoryVal>
            <v1:customerInventoryDate/>
            <v1:customerInventorySource/>
            <v1:inventoryDate/>
            <v1:inventorySource/>
        </v1:InventoryInfo>
    </v1:InventoryServiceResponse>
</soapenv:Body>

当我尝试从上面的XML获取库存级别值时,我无法获取它们。这是我的代码:

Set oXML = CreateObject("MSXML2.DOMDocument")
oXML.Async = False
oXML.Load(sResponse)
oXML.SetProperty "SelectionLanguage", "XPath"
oXML.SetProperty "SelectionNamespaces", "xmlns:soapenv="& chr(34) & _
    "http://schemas.xmlsoap.org/soap/envelope/" & Chr(34) & _
    "  xmlns:v1=" & Chr(34) & "http://us.com/InventoryService/v1/" & Chr(34)

Dim path
path = "/soapenv:Envelope/soapenv:Body/v1:InventoryServiceResponse/v1:InventoryInfo/v1:localQuantityVal"

Set acct = oXML.SelectNodes(path)

For Each objNode In acct
    print objNode.text
Next

1 个答案:

答案 0 :(得分:1)

首先,您的xml没有结束标记Envelope。这应该是xml:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header xmlns:v1="http://us.com/InventoryService/v1/"/>
<soapenv:Body xmlns:v1="http://us.com/InventoryService/v1/">
    <v1:InventoryServiceResponse>
        <v1:InventoryInfo>
            <v1:productIdVal>ToothPick</v1:productIdVal>
            <v1:localQuantityVal>0</v1:localQuantityVal>
            <v1:localPlusQuantityVal>44</v1:localPlusQuantityVal>
            <v1:nationalQuantityVal>1475</v1:nationalQuantityVal>
            <v1:customerInventoryVal>0</v1:customerInventoryVal>
            <v1:customerInventoryDate/>
            <v1:customerInventorySource/>
            <v1:inventoryDate/>
            <v1:inventorySource/>
        </v1:InventoryInfo>
    </v1:InventoryServiceResponse>
</soapenv:Body>
</soapenv:Envelope>

检索值的代码:

Dim objXML, strPath, strNS, strXpath
strPath = "J:\Documents\Work\PersonalWork\Misc Codes\09042018_SO\read.xml"    '<------Change this path correctly
strNS = "xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:v1=""http://us.com/InventoryService/v1/"""

set objXML = createobject("MSXML2.DomDocument")
objXML.async = false
objXML.load strPath
if objXML.parseError = 0 then
    objXML.setProperty "SelectionNamespaces",strNS
    strXpath = "//v1:InventoryServiceResponse/v1:InventoryInfo/v1:productIdVal"
    set objNode = objXML.selectSingleNode(strXpath)
    msgbox objNode.tagName & "--" & objNode.text
else
    Msgbox objXML.ParseError.Reason
end if
set objXML = Nothing

<强>输出:

enter image description here