Sunday, March 6, 2011

Generating XML

There are certain conditions, when you may want to generate xml file, for instance generating xml file for the data extracted from a database.To keep the example simple this program XMLCreatorExample.java generates XML from a list preloaded with hard coded data. The output will be book.xml file with the following content.

<?xml version="1.0" encoding="UTF-8"?>
<Books>
<Book Subject="Java 1.5">
<Author>Kathy Sierra .. etc</Author>
<Title>Head First Java</Title>
</Book>
<Book Subject="Java Architect">
<Author>Kathy Sierra .. etc</Author>
<Title>Head First Design Patterns</Title>
</Book>
</Books>


The steps involved are


  • Load Data
  • Get an instance of Document object using document builder factory
  • Create the root element Books
  • For each item in the list create a Book element and attach it to Books element
  • Serialize DOM to FileOutputStream to generate the xml file "book.xml".

a) Load Data.

/**
* Add a list of books to the list
* In a production system you might populate the list from a DB
*/
private void loadData(){

myData.add(new Book("Head First Java",
"Kathy Sierra .. etc","Java 1.5"));

myData.add(new Book("Head First Design Patterns",
"Kathy Sierra .. etc","Java Architect"));
}



b) Getting an instance of DOM.


/**
* Using JAXP in implementation independent manner create a document object
* using which we create a xml tree in memory
*/
private void createDocument() {

//get an instance of factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
//get an instance of builder
DocumentBuilder db = dbf.newDocumentBuilder();

//create an instance of DOM
dom = db.newDocument();

}catch(ParserConfigurationException pce) {
//dump it
System.out.println("Error while trying to instantiate DocumentBuilder " + pce);
System.exit(1);
}

}



c) Create the root element Books.


/**
* The real workhorse which creates the XML structure
*/
private void createDOMTree(){

//create the root element
Element rootEle = dom.createElement("Books");
dom.appendChild(rootEle);

//No enhanced for
Iterator it = myData.iterator();
while(it.hasNext()) {
Book b = (Book)it.next();
//For each Book object create element and attach it to root
Element bookEle = createBookElement(b);
rootEle.appendChild(bookEle);
}

}


d) Creating a book element.


/**
* Helper method which creates a XML element
* @param b The book for which we need to create an xml representation
* @return XML element snippet representing a book
*/
private Element createBookElement(Book b){

Element bookEle = dom.createElement("Book");
bookEle.setAttribute("Subject", b.getSubject());

//create author element and author text node and attach it to bookElement
Element authEle = dom.createElement("Author");
Text authText = dom.createTextNode(b.getAuthor());
authEle.appendChild(authText);
bookEle.appendChild(authEle);

//create title element and title text node and attach it to bookElement
Element titleEle = dom.createElement("Title");
Text titleText = dom.createTextNode(b.getTitle());
titleEle.appendChild(titleText);
bookEle.appendChild(titleEle);

return bookEle;

}



e) Serialize DOM to FileOutputStream to generate the xml file "book.xml".

/**
* This method uses Xerces specific classes
* prints the XML document to file.
*/
private void printToFile(){

try
{
//print
OutputFormat format = new OutputFormat(dom);
format.setIndenting(true);

//to generate output to console use this serializer
//XMLSerializer serializer = new XMLSerializer(System.out, format);


//to generate a file output use fileoutputstream instead of system.out
XMLSerializer serializer = new XMLSerializer(
new FileOutputStream(new File("book.xml")), format);

serializer.serialize(dom);

} catch(IOException ie) {
ie.printStackTrace();
}
}



Note:
The Xerces internal classes OutputFormat and XMLSerializer are in different packages.
In JDK 1.5 with built in Xerces parser they are under
com.sun.org.apache.xml.internal.serialize.OutputFormat
com.sun.org.apache.xml.internal.serialize.XMLSerializer
In Xerces 2.7.1 which we are using to run these examples they are under
org.apache.xml.serialize.XMLSerializer
org.apache.xml.serialize.OutputFormat
We are using Xerces 2.7.1 with JDK 1.4 and JDK 1.3 as the default parser with JDK 1.4 is Crimson and there is no built in parser with JDK 1.3.
Also please remember it is not advisable to use parser implementation specific classes like OutputFormat and XMLSerializer as they are only available in Xerces and if you switch to another parser in the future you may have to rewrite.


Listing full code:



import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

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

//For jdk1.5 with built in xerces parser
//import com.sun.org.apache.xml.internal.serialize.OutputFormat;
//import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

//For JDK 1.3 or JDK 1.4 with xerces 2.7.1
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;


public class XMLCreatorExample {

//No generics
List myData;
Document dom;

public XMLCreatorExample() {
//create a list to hold the data
myData = new ArrayList();

//initialize the list
loadData();

//Get a DOM object
createDocument();
}


public void runExample(){
System.out.println("Started .. ");
createDOMTree();
printToFile();
System.out.println("Generated file successfully.");
}

/**
* Add a list of books to the list
* In a production system you might populate the list from a DB
*/
private void loadData(){
myData.add(new Book("Head First Java", "Kathy Sierra .. etc","Java 1.5"));
myData.add(new Book("Head First Design Patterns", "Kathy Sierra .. etc","Java Architect"));
}

/**
* Using JAXP in implementation independent manner create a document object
* using which we create a xml tree in memory
*/
private void createDocument() {

//get an instance of factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
//get an instance of builder
DocumentBuilder db = dbf.newDocumentBuilder();

//create an instance of DOM
dom = db.newDocument();

}catch(ParserConfigurationException pce) {
//dump it
System.out.println("Error while trying to instantiate DocumentBuilder " + pce);
System.exit(1);
}

}

/**
* The real workhorse which creates the XML structure
*/
private void createDOMTree(){

//create the root element <Books>
Element rootEle = dom.createElement("Books");
dom.appendChild(rootEle);

//No enhanced for
Iterator it = myData.iterator();
while(it.hasNext()) {
Book b = (Book)it.next();
//For each Book object create <Book> element and attach it to root
Element bookEle = createBookElement(b);
rootEle.appendChild(bookEle);
}

}

/**
* Helper method which creates a XML element <Book>
* @param b The book for which we need to create an xml representation
* @return XML element snippet representing a book
*/
private Element createBookElement(Book b){

Element bookEle = dom.createElement("Book");
bookEle.setAttribute("Subject", b.getSubject());

//create author element and author text node and attach it to bookElement
Element authEle = dom.createElement("Author");
Text authText = dom.createTextNode(b.getAuthor());
authEle.appendChild(authText);
bookEle.appendChild(authEle);

//create title element and title text node and attach it to bookElement
Element titleEle = dom.createElement("Title");
Text titleText = dom.createTextNode(b.getTitle());
titleEle.appendChild(titleText);
bookEle.appendChild(titleEle);

return bookEle;

}

/**
* This method uses Xerces specific classes
* prints the XML document to file.
*/
private void printToFile(){

try
{
//print
OutputFormat format = new OutputFormat(dom);
format.setIndenting(true);

//to generate output to console use this serializer
//XMLSerializer serializer = new XMLSerializer(System.out, format);


//to generate a file output use fileoutputstream instead of system.out
XMLSerializer serializer = new XMLSerializer(
new FileOutputStream(new File("book.xml")), format);

serializer.serialize(dom);

} catch(IOException ie) {
ie.printStackTrace();
}
}


public static void main(String args[]) {

//create an instance
XMLCreatorExample xce = new XMLCreatorExample();

//run the example
xce.runExample();
}
}



See below for steps to make your code JAXP compliant.

 

Alternative to XMLSerializer
It is not advisable to use parser implementation specific classes like OutputFormat and XMLSerializer as they are only available in Xerces and if you switch to another parser in the future you may have to rewrite. To be JAXP compliant use the following to classes to generate the output.


import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

The code to generate xml to a file would be something like

Transformer tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty(OutputKeys.METHOD,"xml");
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");

//to send the output to a file
tr.transform( new DOMSource(dom),new StreamResult(new FileOutputStream("test.xml")));


//to send the output to console
//tr.transform( new DOMSource(dom),new StreamResult(System.out));


What is endorsed standard?
An endorsed standard is a JavaTM API defined through a standards process other than the Java Community ProcessSM (JCPSM). Because endorsed standards are defined outside the JCP, it is anticipated that such standards may be revised between releases of the Java 2 Platform. In order to take advantage of new revisions to endorsed standards, developers and software vendors may use the Endorsed Standards Override Mechanism to provide newer versions of an endorsed standard than those included in the Java 2 Platform as released by Sun Microsystems.
By default java looks for endorsed jar files under


<java-home>\lib\endorsed [Microsoft Windows]
<java-home>/lib/endorsed [Solaris or Linux]

This can be overridden using java system property java.endorsed.dirs as follows
java -Djava.endorsed.dirs=<folder_name>
For more information see Endorsed Standards Override Mechanism

Running XMLCreatorExample ( JDK 1.5+ )


  1. Download XMLCreatorExample.java, Book.java to c:\xercesTest
  2. Go to command prompt and type
    cd c:\xercesTest
  3. To compile, type
    javac -classpath . XMLCreatorExample.java
  4. To run, type
    java -classpath . XMLCreatorExample

No comments:

Post a Comment

Chitika