【问题标题】:How to get properties from a xml response coming from a Rest call如何从来自 Rest 调用的 xml 响应中获取属性
【发布时间】:2014-05-29 12:28:09
【问题描述】:

我在 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 rest grails


    【解决方案1】:

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

    例如

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多