How to Generate XML from RDBMS

| No Comments

After the flip, I list code, that will soon be incorporated into Commons-dbutils, to generate XML from an RDBMS table.

Reblog this post [with Zemanta]


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.NClob;
import java.sql.Ref;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.dbutils.ResultSetHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class XmlHandler implements ResultSetHandler {
    TransformerHandler th = null;
    ByteArrayOutputStream xmlStream;
    ResultSet resultSet = null;
    public XmlHandler() throws TransformerConfigurationException, SAXException {
	SAXTransformerFactory factory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
	th = factory.newTransformerHandler();
	xmlStream = new ByteArrayOutputStream();
	th.setResult(new StreamResult(xmlStream));
	th.startDocument();
    }

    public XmlHandler(ResultSet rs) throws TransformerConfigurationException, SAXException {
	this();
	setResultSet(rs);
    }
    
    public T handle (ResultSet rs) throws SQLException {
	try {
	    th.endDocument();
	} catch (SAXException e) {
	    throw new SQLException(e.getMessage());
	}
	return (T)xmlStream;
    }

    public void setResultSet(ResultSet res) {
	this.resultSet = res;
    }

    private void handleArray(String name, Array obj) throws SAXException,IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "array");
	oos.writeObject(obj);
	th.startElement(null, null, "field",attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray().length);
	th.endElement(null, null, "field");
    }

    private void handleBigDecimal(String name, BigDecimal obj)  throws SAXException,IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Big Decimal");
	oos.writeObject(obj);
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }

    private void handleBlob(String name, Blob obj)  throws SAXException, IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Blob");
	oos.writeObject(obj);
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }

    private void handleBoolean(String name, boolean obj)  throws SAXException, IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Big Decimal");
	oos.writeObject(new Boolean(obj));
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }

    private void handleByte(String name, byte obj)  throws SAXException,IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Byte");
	oos.writeObject(new Byte(obj));
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }

    private void handleClob(String name, Clob obj)  throws SAXException, IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Clob");
	oos.writeObject(obj);
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }

    private void handleDate(String name, Date obj)  throws SAXException,IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Date");
	oos.writeObject(obj);
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }

    private void handleDouble(String name, double obj)  throws SAXException, IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Double");
	oos.writeObject(new Double(obj));
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }

    private void handleFloat(String name, float obj)  throws SAXException, IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Float");
	oos.writeObject(new Float(obj));
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }

    private void handleInt(String name, int obj)  throws SAXException, IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Integer");
	oos.writeObject(new Integer(obj));
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }

    private void handleLong(String name, long obj)  throws SAXException, IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Long");
	oos.writeObject(new Long(obj));
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }

    private void handleNClob(String name, NClob obj)  throws SAXException, IOException {
	this.handleClob(name, obj);
    }

    private void handleNString(String name, String obj)  throws SAXException, IOException {
	this.handleString(name, obj);
    }

    private void handleObject(String name, Object obj)  throws SAXException, SQLException, IOException {
	if (!(obj instanceof Serializable)) {
	    throw new IOException("Only serializable objects supported!");
	} else {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Object");
	oos.writeObject(obj);
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
	}
    }

    private void handleRef(String name, Ref obj)  throws SAXException, IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Ref");
	oos.writeObject(obj);
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }

    private void handleSQLXML(String name, SQLXML obj)  throws SAXException, IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "SQL XML");
	oos.writeObject(obj);
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }

    private void handleString(String name, String obj)  throws SAXException, IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "String");
	oos.writeObject(obj);
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }
    
    private void handleTime(String name, Time obj)  throws SAXException, IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Time");
	oos.writeObject(obj);
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }
    
    private void handleTimestamp(String name, Timestamp obj)  throws SAXException, IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Timestamp");
	oos.writeObject(obj);
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }

    private void handleURL(String name, URL obj)  throws SAXException, IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	AttributesImpl attrs = new AttributesImpl();
	attrs.addAttribute(null, null, null, "name", name);
	attrs.addAttribute(null, null, null, "type", "Big Decimal");
	oos.writeObject(obj);
	th.startElement(null, null, "field", attrs);
	th.characters(new String(baos.toByteArray()).toCharArray(), 0, baos.toByteArray()
		      .length);
	th.endElement(null, null, "field");
    }
}

Leave a comment

Bookmark and Share

Connect with me


qrcode
Add me on AOL
Hasan Diwan

Follow me on Twitter
Ring me at +1 6502844111
See my photos
How about some analytics?

Twitter

Archives

Creative Commons License
This blog is licensed under a Creative Commons License.