4. XML Builder in Groovy

XML (extensible markup language) is one way/ method to dynamically move data from one program to another program. Even though XML makes programmer easy to move data, but programmatically XML is very difficult to implement using most programming language and of course it will take a lot of time for programmer to do it. Fortunately, groovy has made it easier we can create, load(read) or even modify XML document without very long and dizzy code. For example, I’d like to create XML just like previous program(Employee.java) here is the XML output :

	<name>Achmad Fauzi</name>

For reference I’d like to use java code to generate that XML document. I think I takes a lot of time

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class EmployeeXML {

	public static void main(String argv[]) {

	  try {

		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

		// root elements
		Document doc = docBuilder.newDocument();
		Element rootElement = doc.createElement("Employee");

		// name elements
		Element name = doc.createElement("name");
		name.appendChild(doc.createTextNode("Achmad Fauzi"));

		// address elements
		Element address = doc.createElement("address");

		// jobDescription elements
		Element jobDescription = doc.createElement("JobDescription");

		// write the content into xml file
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(new File("D:\\employee.xml"));

		// Output to console for testing
		// StreamResult result = new StreamResult(System.out);

		transformer.transform(source, result);

		System.out.println("File saved!");

	  } catch (ParserConfigurationException pce) {
	  } catch (TransformerException tfe) {

And the result is

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
	<name>Achmad Fauzi</name>

As you can see from the code above it was really – really a long code. Can you imagine if we create long, nested and complicated XML file. How to code will be like ?
Evolution has come, by using groovy we can remove those long codes to be like this

def builder = new groovy.xml.MarkupBuilder()
    name "achmad fauzi"
    address "jember"
    jobDescription "programmer"

That’s it. We only need 6 lines to make that XML document. Absolutely it saves a lot of our time instead of using native java or another programming language.

references : http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/


Tagged: , , , ,

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

%d bloggers like this: