2012-09-24 51 views
0

我要求Google Places API並希望以XML獲取響應。 我這樣做:Grails渲染XML會丟失xml格式和標記

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

它返回的數據,但沒有XML標籤。所以明文數據爲字符串。我百分百確信,我從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。

+0

你'println''d的'resp.getData()',看它是否匹配? –

+0

您確定這不僅僅是您的瀏覽器呈現XML響應的方式 - 請查看「查看源代碼」(或者您在特定瀏覽器中調用的任何內容)。 –

+0

如果xml格式正確,瀏覽器會顯示它。 println顯示沒有標籤的相同輸出。所以基本上是缺少一半的信息。只有所有的價值。 –

回答

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")