groovy将xml解析为pojo

groovy将xml解析为pojo,xml,groovy,pojo,Xml,Groovy,Pojo,我需要得到这个网址(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:

我需要得到这个网址(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>

2722092
大卫·特伦
3380835
1979年路易西亚纳省政府选举
3380827
1983年路易西亚纳省政府选举
2722798
爱德温爱德华滋
1791213
Predefinição:路易西安娜总督
2941389
职业生涯(1959年拍摄)
1969582
女人万岁
2148082
与德国佬共度圣诞节
2077295
悲伤的麻袋
2765563
维特
我非常感谢您的帮助,我不知道该怎么做,因为无法访问,例如json[1].id 谢谢你,
RR

您应该能够使用以下内容:

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

您应该能够使用以下内容:

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

好啊非常感谢你的工作。这确实有效。但是,我想对map_uu函数做一个小的解释。请在每一行后给出一个小的评论,那就可以了:p再次感谢你,不要让@tim_yates用勺子喂你,也许你可以通过阅读他使用的每种方法的文档来了解自己。如果你有什么不明白的地方,就发一个新问题。@recoInrelax添加了一些评论<代码>注入确定。非常感谢你的工作。这确实有效。但是,我想对map_uu函数做一个小的解释。请在每一行后给出一个小的评论,那就可以了:p再次感谢你,不要让@tim_yates用勺子喂你,也许你可以通过阅读他使用的每种方法的文档来了解自己。如果你有什么不明白的地方,就发一个新问题。@recoInrelax添加了一些评论<代码>注入