groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bartoleo <bartolole...@gmail.com>
Subject Re: XML node access by variable
Date Fri, 11 Mar 2016 08:08:36 GMT
One way is using 'index notation':

report.R.each {item ->
  println item[myTag]
}

or GString
report.R.each {item ->
  println item."$myTag"
}

report.R."$myTag".each {item ->
  println item
}

Leo

2016-03-11 5:46 GMT+01:00 Carl Marcum [via Groovy] <
ml-node+s329449n5731831h32@n5.nabble.com>:

> I'm using XML Slurper to parse an XML file with this type of format:
>
> <R>
>    <C0>...</C0>
>    <C1>...</C1>
>    <C2>...</C2>
> </R>
>   <R>
>    <C0>...</C0>
>    <C1>...</C1>
>    <C2>...</C2>
> </R>
> ...
>
> The XML file node names are constantly changing so I would like to use a
> variable for the node designations.
>
> defreport =newXmlSlurper().parse(myXmlFile)
>
> instead of access the C1 node like:
> report.R.each() {item ->
> item.C1
> }
>
> I'm using:
> def myTag = "C1"
> report.R.each {item ->
> item.getProperty(myTag)
> }
>
> Is there a shorter way to access the node by variable without
> getProperty(tagVar)?
>
> Thanks,
> Carl Marcum
> Apache OpenOffice committer and PMC
>
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://groovy.329449.n5.nabble.com/XML-node-access-by-variable-tp5731831.html
> To start a new topic under Groovy Users, email
> ml-node+s329449n329450h21@n5.nabble.com
> To unsubscribe from Groovy, click here
> <http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=329449&code=YmFydG9sb2xlbzc0QGdtYWlsLmNvbXwzMjk0NDl8LTEwMTExMjkyMzk=>
> .
> NAML
> <http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>



-- 
Leo Bartoloni

-----------------------------------------------------------------------------------------------------------------------------------------------------------
Qualsiasi utilizzo non autorizzato del presente messaggio e dei suoi
allegati è vietato e potrebbe costituire reato.
Se lei avesse ricevuto erroneamente questo messaggio, Le saremmo grati se
provvedesse alla distruzione dello stesso
e degli eventuali allegati.
Opinioni, conclusioni o altre informazioni riportate nella e-mail, che non
siano relative alle attività e/o
alla missione aziendale. si intendono non attribuibili alla società stessa,
né la impegnano in alcun modo.




--
View this message in context: http://groovy.329449.n5.nabble.com/XML-node-access-by-variable-tp5731831p5731833.html
Sent from the Groovy Users mailing list archive at Nabble.com.
Mime
View raw message