gpt4 book ai didi

xml - SOAP 响应的命名空间不适用于 XPath

转载 作者:行者123 更新时间:2023-12-04 17:01:52 25 4
gpt4 key购买 nike

我有一个 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

最佳答案

首先,您的 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

关于xml - SOAP 响应的命名空间不适用于 XPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49724838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com