groovy解析xml到pojo
问题描述:
我需要得到这个URL(http:// localhost:8983/solr/select /?q = treen & omitHeader = true)并获取响应为xml,并为每条记录解析它到一个班级,然后创建一个包含所有信息的最终地图。事情是这样的:groovy解析xml到pojo
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>
我会apreciate解决这个问题,我不明白如何能不能做到,因为无法接取,例如,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 { [email protected] == 'id' }.text()
// then, find the arr element with name='title', and get the text() from the str element within
String title = e.arr.find { [email protected] == 'title' }.str.text()
// Then, push this new map entry into our current map
map << [ (id): new WikiDoc(id:id, title:title) ]
}
}
鉴于你的示例XML,在该函数的结束,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) ]
确定。非常感谢你的工作。这实际上是工作。但是,我会为map_函数做一个小小的解释。请在每行之后给出一个小小的评论,这将做到:p再次感谢您 – recoInrelax
而不是要求@tim_yates为您喂食,或许您可以通过阅读他正在使用的每种方法的文档来了解自己。如果有什么你不明白的地方,然后发布一个关于它的新问题。 –
@recoInrelax添加了一些评论。 '注射'[这里描述](http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html#inject%28U,%20groovy.lang.Closure%29) –