помогите с отличным GPATH - PullRequest
2 голосов
/ 29 июня 2011

Мне нужно проанализировать XML-ответ googleReverseGeocoding.Следующий код является примером ответа.

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

Мне нужно найти тег "long_name" со значением "Lisboa", который появляется, когда тегом типа administrator_area_level_1.Я попробовал следующее, но это не работает:

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

Небольшая помощь будет большой.Заранее спасибо.

1 Ответ

4 голосов
/ 29 июня 2011

Попробуйте что-то вроде этого:

records = new XmlSlurper().parseText(xml)
records.result.address_component.find { address ->
    address.type.any { type -> 
        type == 'administrative_area_level_1'
    }
}.long_name
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...