groovy解析xml到pojo

时间:2011-11-30 13:52:19

标签: xml groovy pojo

我需要获取此URL(http:// localhost:8983 / solr / select /?q = treen& omitHeader = true)并将响应作为xml获取,并为每条记录将其解析为一个类,然后创建包含所有信息的最终地图。像这样:

 class SolrController {

    def solr= {
        def map_ = [:]

        def json = grails.converters.XML.parse( new URL( 'http://localhost:8983/solr/select/?q=treen&omitHeader=true' ).text)

        json.each{
            Wikidoc aa = new wikiDoc(id: it.id, title: it.title)
            map_.put(aa.id, aa)
        }
    }
}

class wikiDoc{
    String id
    String title
}

xml如下:

<response>
<result name="response" numFound="18" start="0">
<doc>
<str name="id">2722092</str>
<arr name="title">
<str>David Treen</str>
</arr>
</doc>
<doc>
<str name="id">3380835</str>
<arr name="title">
<str>Eleição para governador da Luisiana em 1979</str>
</arr>
</doc>
<doc>
<str name="id">3380827</str>
<arr name="title">
<str>Eleição para governador da Luisiana em 1983</str>
</arr>
</doc>
<doc>
<str name="id">2722798</str>
<arr name="title">
<str>Edwin Edwards</str>
</arr>
</doc>
<doc>
<str name="id">1791213</str>
<arr name="title">
<str>Predefinição:Governadores da Luisiana</str>
</arr>
</doc>
<doc>
<str name="id">2941389</str>
<arr name="title">
<str>Career (filme de 1959)</str>
</arr>
</doc>
<doc>
<str name="id">1969582</str>
<arr name="title">
<str>Kitty Foyle</str>
</arr>
</doc>
<doc>
<str name="id">2148082</str>
<arr name="title">
<str>Christmas with the Kranks</str>
</arr>
</doc>
<doc>
<str name="id">2077295</str>
<arr name="title">
<str>The Sad Sack</str>
</arr>
</doc>
<doc>
<str name="id">2765563</str>
<arr name="title">
<str>David Vitter</str>
</arr>
</doc>
</result>
</response>

我会对此提供帮助,我不知道怎么做,因为无法进行,例如,json [1] .id 谢谢你, RR

1 个答案:

答案 0 :(得分:1)

你应该可以使用类似的东西:

def solr= {
  def json = grails.converters.XML.parse( new URL( 'http://localhost:8983/solr/select/?q=treen&omitHeader=true' ).text )

  // Start with [:] for every doc element e perform the closure
  def map_ = json.result.doc.inject( [:] ) { map, e ->

    // For this doc element, find the str element with name='id', and get its text()
    String id = e.str.find { it.@name == 'id' }.text()

    // then, find the arr element with name='title', and get the text() from the str element within
    String title = e.arr.find { it.@name == 'title' }.str.text()

    // Then, push this new map entry into our current map
    map << [ (id): new WikiDoc( id:id, title:title ) ]
  }
}

鉴于您的示例x​​ml,在该函数的末尾,map_应该等于:

[ "2722092":WikiDoc(2722092, David Treen),
  "3380835":WikiDoc(3380835, Eleição para governador da Luisiana em 1979),
  "3380827":WikiDoc(3380827, Eleição para governador da Luisiana em 1983),
  "2722798":WikiDoc(2722798, Edwin Edwards),
  "1791213":WikiDoc(1791213, Predefinição:Governadores da Luisiana),
  "2941389":WikiDoc(2941389, Career (filme de 1959)),
  "1969582":WikiDoc(1969582, Kitty Foyle),
  "2148082":WikiDoc(2148082, Christmas with the Kranks),
  "2077295":WikiDoc(2077295, The Sad Sack),
  "2765563":WikiDoc(2765563, David Vitter) ]