首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >groovy GPATH的帮助

groovy GPATH的帮助
EN

Stack Overflow用户
提问于 2011-06-30 00:33:59
回答 1查看 278关注 0票数 2

我需要解析一个googleReverseGeocoding XML响应。以下代码是一个响应示例。

代码语言:javascript
复制
<GeocodeResponse>
<status>OK</status>
<result>...</result>
<result>
  <type>sublocality</type>
  <type>political</type>
  <formatted_address>Oeiras, Portugal</formatted_address>
  <address_component>
    <long_name>Oeiras</long_name>
    <short_name>Oeiras</short_name>
    <type>sublocality</type>
    <type>political</type>
  </address_component>
  <address_component>
    <long_name>Lisboa</long_name>
    <short_name>Lisboa</short_name>
    <type>administrative_area_level_1</type>
    <type>political</type>
  </address_component>
<result>...</result>
<result>...</result>
</GeocodeResponse>

我需要找到值为"Lisboa“的"long_name”标签,它在类型标签为administrative_area_level_1时出现。我尝试了以下方法,但不起作用:

代码语言:javascript
复制
result.city = geocodeResponse.result.find{it.address_component.type == "administrative_area_level_1"}.address_component.find{it.type == "administrative_area_level_1"}.long_name as String

如果能帮上一点忙就好了。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-30 01:21:37

尝试如下所示:

代码语言:javascript
复制
records = new XmlSlurper().parseText(xml)
records.result.address_component.find { address ->
    address.type.any { type -> 
        type == 'administrative_area_level_1'
    }
}.long_name
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6524106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档