Code, JavaEE

Output XML Using Groovy and JDOM

Here is the groovy version:


import org.jdom.*
import org.jdom.output.XMLOutputter

def map = ["Java":"server", "Groovy":"server", "JavaScript":"web"]
def root = new Element('dataset')
def document = new Document(root)
document.setRootElement(root)

map.each { k, v ->
 def elem = new Element(k).setText(v)
 root.addContent(elem)
}

def writer = new StringWriter()
new XMLOutputter().output(document, writer)
println(writer.toString())

 

The output from the console is:


<?xml verion="1.0" encoding="UTF-8"?>

<dataset>

<Java>server</Java>

<Groovy>server</Groovy>

<JavaScript>server</JavaScript>

</dataset>

&nbsp;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s