Groovy
Creating XML with MarkupBuilder
The team of @leveluplunch shows how to create XML with the aid of MarkupBuilder. Whether it is a simple document or a document with attributes. Moreover, he shows a handy feature: omit empty or null attributes.
Generate XML from map
Many times you will have the information in a map. Building an XML document with a Map as input is a piece of cake.
Creating XML with MarkupBuilder
@mrhaki expands on the use of MarkupBuilder.
mkp is a special namespace used to escape away from the normal building mode of the builder and get access to helper markup methods such as 'yield'
Check out how he uses the yield method to generate a p element.
Create sitemap with MarkupBuilder
@jstnm shows how to generate a sitemap.xml file. A must if you are hosting any website and you want to improve your SEO game. Moreover, he shows how to add a prefix with namespace with the aid of StreamingMarkupBuilder
Pretty Print XML
@mrhaki shows how to prettify a XML string. When would you use this? In Unit tests. Let's say that you have a variable with the expected value; an XML string which you have formatted nicely using multiline string capabilities of Groovy. However, the subject under test returns a really long one line XML output. No problem, you make the output pretty and compare them.
Reading XML in groovy using XmlParser
Wheter you read your XML from a String, a File or a File Input Stream, @thecoderscorner shows you how to build an object tree from XML with XmlParser. Learn how to access elements or attributes alternating dot notation or @ notation.
Writing Groovy's groovy.util.Node (XmlParser) Content as XML
Given XML parsed with XmlParser, @DustinMarx shows how to use XmlNodePrinter to:
- Write XML to Standard Output
- Write XML to Java String
- Write XML to File
Writing Groovy's GPathResult (XmlSlurper) Content as XML
Given XML parsed with XmlSlurper, @DustinMarx shows how to use XmlNodePrinter to:
- Write XML to Standard Output
- Write XML to Java String
- Write XML to File
Processing XML Files with Groovy: Creating
I like posts such as this one by @JeffJJensen. They put words to the magic:
- Literal names in the closure structure become the element names in the XML file.
- Expressions inside the parenthesis are evaluated and used as the elementโs value.
- Elements inside the curly braces become the next nested level inside the named element (they are each closures โ anonymous closures as they are not bound to variables โ and are evaluated at runtime).
- Specify the attributes inside parenthesis, comma separated, followed by an optional value expression
- Specify an attribute with its name, a colon, and its value-expression.
Groovy Xml processing
Mohamed Fawy covers:
- XML reading in Java vs Groovy
- DOM Category
- XML Parser vs XML Slurper
- Markup builder vs Streaming Markup Builder
Groovy Calamari Support
Groovy Calamari T-shirt
A little bit of trivia. Today is 6th of January. It is a day to exchange presents in Spain. We don't celebrate Santa Claus but the Three Wise Men instead.
Speaking of presents. Are you based in the US ๐บ๐ธ ? If you are, dress smartly and support Groovy Calamari by gifting yourself a T-Shirt. Thanks in advanced.
Sponsored Jobs
Grails Fullstack developer in Agora Pulse - Paris
Agora Pulse is looking for a backend Developer to help them continue the development of their Social Media Management Saas.
They have done a tremendous job in the past for the community releasing plugins to interact with AWS Services or the NewRelic plugin.
If you are looking for a Bootstraped startup, working with Grails and doing innovative work with AWS services, Agora Pulse it is. Moreover, who would not like to live in Paris ๐ซ๐ท ?
Comment
Happy new year!
Ready for GR8Conf India ? It starts tomorrow and the tracks look interesting. I will probably link to some of its content in the next weeks.
In the world of JSON, I wanted to use this issue to talk about XML ๐. Learn how to read and write XML with Groovy. It does not matter if it is XML with attributes, weird namespaces, optional values etc. It is covered in this issue.
Sergio del Amo