You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Carl Marcum <cm...@apache.org> on 2016/03/11 05:45:34 UTC

XML node access by variable

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


Re: XML node access by variable

Posted by Carl Marcum <cm...@apache.org>.
On 03/11/2016 03:08 AM, bartoleo wrote:
> 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] <[hidden email] 
> </user/SendEmail.jtp?type=node&node=5731833&i=0>>:
>
>     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 [hidden email]
>     </user/SendEmail.jtp?type=node&node=5731833&i=1>
>     To unsubscribe from Groovy, click here.
>     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: Re: XML node access by variable 
> <http://groovy.329449.n5.nabble.com/XML-node-access-by-variable-tp5731831p5731833.html>
> Sent from the Groovy Users mailing list archive 
> <http://groovy.329449.n5.nabble.com/Groovy-Users-f329450.html> at 
> Nabble.com.

That's great!

Thanks Leo !!

Carl

Re: XML node access by variable

Posted by bartoleo <ba...@gmail.com>.
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.