Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Apache Groovy
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===JSON and XML processing=== On JavaScript Object Notation ([[JSON]]) and XML processing, Groovy employs the [[Builder pattern]], making the production of the data structure less verbose. For example, the following XML: <syntaxhighlight lang="xml"> <languages> <language year="1995"> <name>Java</name> <paradigm>object oriented</paradigm> <typing>static</typing> </language> <language year="1995"> <name>Ruby</name> <paradigm>functional, object oriented</paradigm> <typing>duck typing, dynamic</typing> </language> <language year="2003"> <name>Groovy</name> <paradigm>functional, object oriented</paradigm> <typing>duck typing, dynamic, static</typing> </language> </languages> </syntaxhighlight> can be generated via the following Groovy code: <syntaxhighlight lang="groovy"> def writer = new StringWriter() def builder = new groovy.xml.MarkupBuilder(writer) builder.languages { language(year: 1995) { name "Java" paradigm "object oriented" typing "static" } language (year: 1995) { name "Ruby" paradigm "functional, object oriented" typing "duck typing, dynamic" } language (year: 2003) { name "Groovy" paradigm "functional, object oriented" typing "duck typing, dynamic, static" } } </syntaxhighlight> and also can be processed in a streaming way through <code>StreamingMarkupBuilder</code>. To change the implementation to JSON, the <code>MarkupBuilder</code> can be swapped to <code>JsonBuilder</code>.<ref>{{cite web |url=http://groovy.codehaus.org/gapi/groovy/json/JsonBuilder.html |title=JsonBuilder |access-date=2012-10-07 |archive-url=https://web.archive.org/web/20121002221510/http://groovy.codehaus.org/gapi/groovy/json/JsonBuilder.html |archive-date=2012-10-02 |url-status=dead }}</ref> To parse it and search for a functional language, Groovy's <code>findAll</code> method can serve: <syntaxhighlight lang="groovy"> def languages = new XmlSlurper().parseText writer.toString() // Here is employed Groovy's regex syntax for a matcher (=~) that will be coerced to a // boolean value: either true, if the value contains our string, or false otherwise. def functional = languages.language.findAll { it.paradigm =~ "functional" } assert functional.collect { it.name } == ["Groovy", "Ruby"] </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)