[英]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. 我需要获取此url(http:// localhost:8983 / solr / select /?q = treen&omitHeader = true)并以xml格式获取响应,并为每条记录将其解析为一个类,然后使用所有信息。 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: 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>
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 我希望对此有所帮助,但我看不到如何完成,因为无法访问,例如json [1] .id感谢adv,RR
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: 给定您的示例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) ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.