gpt4 book ai didi

xml - 如何从来自Rest调用的xml响应中获取属性

转载 作者:行者123 更新时间:2023-12-02 14:43:19 27 4
gpt4 key购买 nike

我在grails中使用rest-client-builder:2.0.1插件。

我正在调用带有xml响应的rest url,并且我也正在获取xml响应。

这是我的休息电话,

def url = "http://ab-rest/getDetails"

def xmlBody = "<someGrp><id>CAP00001-1</id><Name>XX642105YP</Name>"
xmlBody = xmlBody+"<Grade>1</Grade><AccessCode></AccessCode>"
xmlBody = xmlBody+"<productCode>ABC</productCode>"
xmlBody = xmlBody+"<imageuri>www.abcd.com/232134</imageuri></imageuris><someGrp>"

log.debug "image processor xmlBody: ${xmlBody}"

def resp = rest.post(url) {
header "Content-Type", "application/xml"
header "X-Requested-With", "XMLHttpRequest"
header "X-LTCallingApplicationName", "ABC"
header "X-LTCallingUser", "TEST"
header "X-LTCallingApplicationInstance", "System"
header "X-LTCallingApplicationId", "70"
xml xmlBody
}
resp.xml instanceof GPathResult

log.debug " resp.status "+resp.status
log.debug "image processor response xml: ${resp.xml}"

如果我在这里执行resp.status,它将返回200,但是response.xml只是以串联形式返回属性值。像下面一样
ID01-1CAP01-1http://qa.imagecache.cir.lifetouch.net/imagecache/service/imagecache/didimage/bd56a1783eb32b88a31964888cbba066d58961a4200jpg

但是预期的xml是
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<images>
<image>
<id>ID01-1</id>
<capturesession>CAP01-1</capturesession>
<uri>http://ab-rest.net/imagecache/didimage/bd56a1783eb32b88a31964888cbba066d58961a4</uri>
<status>200</status>
<filetype>jpg</filetype>
</image>
</images>

当我输入一个resp.text时,我将以下内容作为字符串输入,
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><images><image><id>ID01-1</id><capturesession>CAP01-1</capturesession><uri>http://ab-rest.net/imagecache/didimage/bd56a1783eb32b88a31964888cbba066d58961a4</uri><status>200</status><filetype>jpg</filetype></image></images>

现在,我想从响应中获取属性,例如,如果我想获取uri,该怎么做?

最佳答案

它返回一个已解析的xml,检查该类,它将是XmlSlurper。

例如

resp.xml.image.find {
log.debug it.uri
}

关于xml - 如何从来自Rest调用的xml响应中获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23933342/

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