Grails渲染XML丢失xml格式和标签

时间:2012-09-24 02:31:11

标签: xml grails

我请求Google Places API,并希望以XML格式获取响应。 我这样做:

def http = new HttpURLClient( )
def resp = http.request(url:uRL)
render(text: resp.getData(), encoding:"UTF-8", contentType:"text/xml")

它返回数据,但没有XML标记。所以普通数据为字符串。我100%肯定,我从Google获得了XML格式的数据。谷歌的回复看起来像(这显示了浏览器):

<PlaceSearchResponse>
<status>OK</status>
<result>
<name>Beth-El Synagogue</name>
<vicinity>Elizabeth Av & Downing, St. John's</vicinity>
<type>synagogue</type>
<type>place_of_worship</type>
<type>establishment</type>
<geometry>
<location>
<lat>47.5799640</lat>
<lng>-52.7172920</lng>
</location>
</geometry>
<icon>
http://maps.gstatic.com/mapfiles/place_api/icons/worship_jewish-71.png
</icon>
<reference>
CnRwAAAAH3oJJ3PaJPk5kesLiioompBTCh2NUBDxAh-wUKwZkFeolqoDCOEoOTYe9UpUdhVhkqMoL9mPPd-C0PuIvkyqBwfvdli1zHvaaEfJklQBHd-haHfMceF4vlPxV5r9kaSyWTwOAGSJWQhT6pkXmmoMCBIQbP457ZvQWJsX0JIEc-DnORoU_NRRZIlZN08azb3UL_X-xpqS6N4
</reference>
<id>b61e059f04e643fce7f4750a42c686901096bab3</id>
</result>
<html_attribution>
Listings by <a href="http://www.yellowpages.ca/">YellowPages.ca</a>
</html_attribution>
</PlaceSearchResponse>

我只是想再次呈现回复。那么,顶部的代码有什么问题? THX。

1 个答案:

答案 0 :(得分:0)

首先将XML解析为文本:

import groovy.xml.XmlUtil
....
def http = new HttpURLClient( )
def resp = http.request(url:uRL)
def xmlAsText = XmlUtil.serialize(resp.data)
render(text: xmlAsText, encoding:"UTF-8", contentType:"text/xml")