Search code examples
xmlgroovypojo

groovy parse xml into pojo


i need to get this url (http://localhost:8983/solr/select/?q=treen&omitHeader=true) and get the response as xml, and for each record, parse it to a class and then create a final map with all the info. Something like this:

 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
}

The xml is the following:

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

I would apreciate help with this, i don't see how can it be done, because can't acess, for example, json[1].id Thanks in adv, RR


Solution

  • You should be able to use something like:

    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 ) ]
      }
    }
    

    Given your example xml, at the end of that function, map_ should be equal to:

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