30 Jan 2018

How to parse Groovy Objects to XML and XML to Groovy Objects

4:48 PM java 0 Comment

In Groovy we can convert the Groovy beans into the XML file and XML file into the Groovy beans.

Here I described and provided the example to convert the Groovy into XML and XML into the Groovy.

Converting the Groovy bean into the XML file:

Groovy provided two techniques to convert the Groovy beans into the XML file. Those are

  • groovy.xml.MarkupBuilder
  • groovy.xml.StreamingMarkupBuilder

There are three key differences between MarkupBuilder and StreamingMarkupBuilder:

  • MarkupBuilder sends its output to System.out by default; StreamingMarkupBuilder is silent until you explicitly hand it off to a Writer.
  • MarkupBuilder is synchronous; StreamingMarkupBuilder is asynchronous. In other words, MarkupBuilder writes the XML document out immediately. StreamingMarkupBuilder allows you to define the closure separately. The document is not generated until the StreamingMarkupBuilder is passed to a Writer.
  • Finally, MarkupBuilder pretty-prints its output, whereas StreamingMarkupBuilder does not.

Example:

Here we have used the StreamingMarkupBuilder to convert Groovy bean into the XML.

Following are the Groovy Beans:

Company.groovy

Product.groovy

Employee.groovy

Following is the service to create the Company instances:

CompanyService.groovy

CompanyServiceImpl.groovy

Now, let’s have trail to generate XML file from the Groovy bean

Here we are given the client with the normal Groovy main method.

CompanyParsingGroovytoXMLTest.groovy

The generated output XML file is looks like as follows,

companyGroovy.xml

We can deal with the Namesapces, XML declarations and also we can change the default encoding value (See the client code above).

Converting the XML file into the Groovy bean:

Groovy provided two techniques to convert the XML file into the Groovy beans. Those are

  • groovy.util.XmlParser
  • groovy.util.XmlSlurper

Difference between XmlParser and XmlSlurper:

Their APIs are almost identical, which is a never-ending source of confusion. (“What is the difference?” “Which one should I use?”) The answer is, of course, that they don’t do exactly the same thing. They are both XML parsing libraries, but each takes a slightly different approach to the problem.

An XmlParser thinks of the document in terms of nodes. When you start dealing with more complex XML documents in just a moment, XmlParser will return a List of nodes as you navigate the tree.

XmlSlurper, on the other hand, treats the document as a groovy.util. slurpersupport.GPathResult. (Since GPathResult is an abstract class, you can see groovy.util.slurpersupport.NodeChild show up as the implementation.) GPath is like XPath,1 only with a “groovier” syntax. XPath uses slash notation to navigate deeply nested XML trees—GPath uses dots to do the same thing.

Example:

Here we have used the groovy.util.XmlParser to convert the XML into the Groovy bean.

For converting the XML into the Groovy bean, we have used the above groovy beans.

→ Following is the XML file which we want to convert to the Groovy bean:

company.xml

→ Following is the client program with main method:

CompanyParsingXMLtoGroovyTest.groovy

The test above will print the Company Object.

→ We can deal with XML namespaces as well by using the class groovy.xml.Namespace.

    We need to create the Namespace instance with the namespace URI like follows

def comp = new groovy.xml.Namespace(“http://uri-of-the-namespace/“)

    And need to invoke the values like follows

company[comp.name.text()]

For more information on Groovy like syntaxes, concepts see reference

For more information on Groovy like classes, methods see Goovy API

 

Thank You 🙂

Leave a Reply