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

时间:2014-05-29 12:28:09

标签: xml rest grails

我在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如何做到这一点?

1 个答案:

答案 0 :(得分:1)

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

e.g。

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