XML Parsing with same element tags but unique data -
i parsing xml has many of same tag. inside tag there name of value , value. issue is need separate values.
this example of xml have parse
<event> <ipaddress> 10.0.0.0. </ip> <sourcesystem> somwhere </sourcesystem> <username> fred91 </username> <user> <username> fred91 </username> <account> fredsaccount </account> <id> f2234232 </id> </user> ////below portion of xml having issues notice multple of same element tag name , values unique///// <event-details> <name>prairie farms</name> <value>summer lane happy land usa</value> </event-details> <event-details> <name>house on fire</name> <value>ashes</value> </event-details> </event>
notice name of events change , not related. if related easier parse. name name of event , unique. value linked unique name. problem trying figure out how parse out unique name generic element tag , assign name corresponding value. told try use collection not sure how go doing that. right have parsing out this.
sql.query(myquery, []) { resultset rs -> while (rs.next()) { def parsing = new xmlslurper().parsetext(auditdetails) def audit = new audit() audit.ipaddress = parsing."ip-address".text() audit.sourcesystem = parsing."source-system".text() audit.username = parsing."username".text() parsing."user".each{ audit.username = parsing."user"."username".text() audit.account = parsing."user"."account".text() audit.id= parsing."user"."id".text() } /*if (parsing."event-details"."name".text().equals("createdtime")){ audit.name = parsing."event-details"."name".text audit.createdtime = parsing."event-details"."value".text() }*/ audit.name = parsing."event-details".name?.text() audit.value = parsing."event-details".value?.text() audit.eventdetails = parsing."event-details".text()
with audit being object , parsing being xmlslurper. right gives me "name" elements 1 string , there corresponding (in order respectively) values in string. this:
name: prairie farmhouse on fire
value: summer lane happy land usaashes
i need like
prairie farms: summer lane happy land usa
house on fire: ashes
i have 10 different unique names know of far (found looking @ query receives them strung together)
what have write picks out unique event names , links value.
parsing.'event-details'.each{ if (parsing."event-details"."name".text().equals("createdtime")){ audit.name = parsing."event-details"."name".text audit.createdtime = parsing."event-details"."value".text() } }
^ first thought, going known unique names , going through , assigning name value way. of linked createdtime value , ignore rest test didn't far.
any suggestions or opinions?
sorry if confusing. find kind of hard explain
thanks time!
assuming using xmlslurper, try:
def audit = parsing.'event-details'.iterator().collectentries { [ it.name, it.value ] }
edit:
ok, given xml:
def x = '''<event> | <ipaddress>10.0.0.0</ipaddress> | <sourcesystem>somwhere</sourcesystem> | <username>fred91</username> | <user> | <username>fred91</username> | <account>fredsaccount</account> | <id>f2234232</id> | </user> | <event-details> | <name>prairie farms</name> | <value>summer lane happy land usa</value> | </event-details> | <event-details> | <name>house on fire</name> | <value>ashes</value> | </event-details> |</event>'''.stripmargin()
and audit class:
@groovy.transform.tostring(includenames=true) class audit { string ipaddress, sourcesystem, username, account, id map eventdetails }
you can do:
def audit = new xmlslurper().parsetext( x ).with { xml -> new audit( ipaddress : xml.ipaddress, sourcesystem : xml.sourcesystem, username : xml.username, account : xml.user.account, id : xml.user.id, eventdetails : xml.'event-details'.iterator().collectentries { [ it.name, it.value ] } ) } println audit
to print:
audit(ipaddress:10.0.0.0, sourcesystem:somwhere, username:fred91, account:fredsaccount, id:f2234232, eventdetails:[prairie farms:summer lane happy land usa, house on fire:ashes])
note:
if using groovy < 1.8.7, change
eventdetails : xml.'event-details'.iterator().collectentries { [ it.name, it.value ] } )
to
eventdetails : xml.'event-details'.inject( [:] ) { m, e -> m << [ (e.name): e.value ] } )
Comments
Post a Comment