gpt4 book ai didi

xml - Grails RestClient访问POST响应XML

转载 作者:行者123 更新时间:2023-12-02 14:42:09 28 4
gpt4 key购买 nike

我正在使用Groovy的RESTClient()执行带有一些XML的POST作为URL的有效负载。我期望回覆是

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope
xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getNetworkSessionsForUsernameResponse
xmlns:ns2="http://webservice.foo.bar.net/">
<return>
<ip>1.1.1.1</ip>
<platform>112</platform>
<pop>ABC</pop>
<site>LIP</site>
<state>STATUS</state>
<userId>ID1234</userId>
</return>
</ns2:getNetworkSessionsForUsernameResponse>
</S:Body>
</S:Envelope>

我已经缩略到URL并知道响应是正确的,因此返回的数据不是问题。我的问题是 RESTClient().post().getData()返回的格式。如果我做
def restClient = new RESTClient("www.someRestService.com", 'text/xml')

String soapRequest = """XML Payload..."""

def restClientResponse = restClient.post(body: soapRequest).getData()

log.info("RESPONSE: " + restClientResponse)
log.info("BODY: " + restClientResponse.Body.getNetworkSessionsForUsernameResponse.return.userId)
log.info("BODY T: " + restClientResponse.Body.getNetworkSessionsForUsernameResponse.return.userId.text())
log.info("NETWORK: " + restClientResponse.getNetworkSessionsForUsernameResponse.return.userId)
log.info("NETWORK T: " + restClientResponse.getNetworkSessionsForUsernameResponse.return.userId.text())
log.info("RETURN: " + restClientResponse.return.userId)
log.info("RETURN T: " + restClientResponse.return.userId.text())
log.info("USERID: " + restClientResponse.userId)
log.info("USERID T: " + restClientResponse.userId.text())
log.info("CHILDREN SIZE: " + restClientResponse.children().size())
log.info("CLASS: " + restClientResponse.getClass().getName())

记录的是:
RESPONSE: 1.1.1.1112ABCLIPSTATUSID1234
BODY:
BODY T:
NETWORK:
NETWORK T:
RETURN:
RETURN T:
USERID:
USERID T:
CHILDREN SIZE: 1
CLASS: groovy.util.slurpersupport.NodeChild

因此, getData()似乎只是返回所有文本节点,而不是已解析的XML对象,我确定我读过某个地方 getData()使用 XMLSlurper()解析XML。我读过 this文章,似乎与我的问题很相似,但是给出的答案正是我在做什么,不是吗?我是否以完全错误的方式访问 restClientResponse

最佳答案

RESTClient在内部用XmlSlurper执行的内部XML标记可能是non namespace aware:

以便,

def restClientResponse = new XmlSlurper(false, false).parseText('''<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope
xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getNetworkSessionsForUsernameResponse
xmlns:ns2="http://webservice.foo.bar.net/">
<return>
<ip>1.1.1.1</ip>
<platform>112</platform>
<pop>ABC</pop>
<site>LIP</site>
<state>STATUS</state>
<userId>ID1234</userId>
</return>
</ns2:getNetworkSessionsForUsernameResponse>
</S:Body>
</S:Envelope>''')

println restClientResponse
def userId
userId = restClientResponse.Body.getNetworkSessionsForUsernameResponse.return.userId
println "${userId.size()}: [${userId.text()}]"
userId = restClientResponse.'S:Body'.'ns2:getNetworkSessionsForUsernameResponse'.return.userId
println "${userId.size()}: [${userId.text()}]"

以上 yield :
1.1.1.1112ABCLIPSTATUSID1234
0: []
1: [ID1234]

(将结果更改为可识别 namespace 的 XmlSlurper则相反)。

关于xml - Grails RestClient访问POST响应XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29305506/

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