0

我请求 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>

我只想再次呈现响应。那么,上面的代码有什么问题呢?谢谢。

4

1 回答 1

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")
于 2013-03-11T09:29:49.943 回答