cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chan Mei Theng" <meith...@pocketgroup.co.uk>
Subject HSSFSerializer ( XML2XLS Converter )
Date Tue, 03 Jul 2007 07:39:56 GMT
Hi,

 

Here is the java backend program for converting XML to XLS using
HSSFSerializer.

 

XML - XSL - XML

=============

Input - XML

Input - XSL ( embedded with Gnumeric )

Output - XML ( Gnumeric )

 

 

import java.io.FileNotFoundException;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

 

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.stream.StreamResult;

import javax.xml.transform.stream.StreamSource;

 

/**

 * converts an XML file into another XML file based on XSL stylesheet

 *

 */

public class XML2XMLTransformer {

                public static org.apache.log4j.Logger logger =
org.apache.log4j.Logger.getLogger("XML2XMLTransformer");

                

                /**

     * converts an input XML file to a new XML file based on an XSL
stylesheet

     * 

     * @throws Exception

     * 

     */

                public void convert(java.io.InputStream isXML,
java.io.InputStream isXSL, java.io.OutputStream osXML) 

                                                throws Exception,
TransformerException, TransformerConfigurationException,
FileNotFoundException, IOException {

                                javax.xml.transform.TransformerFactory
xmlFactory = null;

                                javax.xml.transform.Transformer
xmlTransformer = null;

                                javax.xml.transform.Source xslSource = null;

                                javax.xml.transform.Source xmlSource = null;

                                javax.xml.transform.Result xmlResult = null;

 

                                try

                                {

                                                xslSource = new
javax.xml.transform.stream.StreamSource(isXSL);

                                                xmlSource = new
javax.xml.transform.stream.StreamSource(isXML);

                                                xmlResult = new
javax.xml.transform.stream.StreamResult(osXML);

 

                                                xmlFactory =
javax.xml.transform.TransformerFactory.newInstance();

//                                              xmlTransformer =
xmlFactory.newTransformer(new
javax.xml.transform.stream.StreamSource("C:\\eclipse-SDK\\eclipse-3.2.2\\wor
kspace\\Testing\\WEB-INF\\classes\\page2xls.xsl"));

                                                xmlTransformer =
xmlFactory.newTransformer(xslSource);

//                                              xmlTransformer.transform(new
StreamSource("C:\\eclipse-SDK\\eclipse-3.2.2\\workspace\\Testing\\WEB-INF\\c
lasses\\hello.xml"), new StreamResult(new
FileOutputStream("C:\\eclipse-SDK\\eclipse-3.2.2\\workspace\\Testing\\WEB-IN
F\\classes\\result.xml")));

 
xmlTransformer.transform(xmlSource, xmlResult);

                                }

                                catch (Exception ex)

                                {

 
logger.fatal(ex.getMessage());

                                                ex.printStackTrace();

                                } finally {

                                                xmlFactory = null;

                                                xmlTransformer = null;

                                                xmlSource = null;

                                                xslSource = null;

                                                xmlResult = null;

                                }

    }

 

    public static void main(String[] args) throws FileNotFoundException,
Exception {

                                org.apache.log4j.PatternLayout loggerLayout
= null;

                                org.apache.log4j.WriterAppender
loggerAppender = null;

                                java.io.FileOutputStream loggerOutput =
null;

 

                                try {

                                                loggerOutput = new
java.io.FileOutputStream("C:\\eclipse-SDK\\eclipse-3.2.2\\workspace\\Testing
\\WEB-INF\\classes\\XML2XMLTransfomer.log");

                                                loggerLayout = new
org.apache.log4j.PatternLayout();

                                                loggerAppender = new
org.apache.log4j.WriterAppender(loggerLayout, loggerOutput);

 
logger.addAppender(loggerAppender);

 
logger.setLevel(org.apache.log4j.Level.INFO);

 

                                                if (args.length != 3) {

 
System.err.println("Usage:");

 
System.err.println(" java XML2XMLTransformer <input:XMLFileName>
<input:xsltFileName> <output:xmlFileName>");

 

 
logger.info("Incorrect input parameters.");

 

 
System.exit(1);

                                                }

 

 

 
logger.info("XML2XMLTransformer::START");

 

                                                new
XML2XMLTransformer().convert(new java.io.FileInputStream(args[0]), new
java.io.FileInputStream(args[1]), new java.io.FileOutputStream(args[2]));

 

 
logger.info("XML2XMLTransformer::END");

                                } catch(Exception ex) {

 
logger.fatal(ex.getMessage());

                                                ex.printStackTrace();

                                }

                }

}

 

 

XML - XLS with HSSFSerializer

=======================

Input - XML ( Gnumeric )

Output - XLS

 

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

 

import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.TransformerException;

 

/**

 * converts an xml stream into an xls stream with HSSFSerializer

 *

 */

public class XML2XLSTransformer {

                public static org.apache.log4j.Logger logger =
org.apache.log4j.Logger.getLogger("XML2XLSTransformer");

                

 

                /**

     * converts an input XML file to an xls file

     * 

     * @throws Exception

     * 

     */

    public void convert(InputStream is, OutputStream os)

                                                throws Exception,
TransformerException, TransformerConfigurationException,
FileNotFoundException, IOException {

 
org.apache.cocoon.serialization.HSSFSerializer hs = null;

//                              java.io.OutputStream os = null;

                                javax.xml.transform.TransformerFactory
xmlFactory = null;

                                javax.xml.transform.Transformer
xmlTransformer = null;

                                javax.xml.transform.Source xmlSource = null;

                                javax.xml.transform.Result xlsResult = null;

 

                                try

                                {

                                                hs = new
org.apache.cocoon.serialization.HSSFSerializer();

                                                hs.enableLogging(new
org.apache.avalon.excalibur.logger.Log4JLogger(logger));

                                                hs.initialize();

//                                              os = new
java.io.FileOutputStream("C:\\eclipse-SDK\\eclipse-3.2.2\\workspace\\Testing
\\WEB-INF\\classes\\result.xls"); 

                                                hs.setOutputStream(os);

 

                                                xmlFactory =
javax.xml.transform.TransformerFactory.newInstance();

                                                xmlTransformer =
xmlFactory.newTransformer();

//                                              xmlSource = new
javax.xml.transform.stream.StreamSource("C:\\eclipse-SDK\\eclipse-3.2.2\\wor
kspace\\Testing\\WEB-INF\\classes\\gnumeric.xml");

                                                xmlSource = new
javax.xml.transform.stream.StreamSource(is);

                                                xlsResult = new
javax.xml.transform.sax.SAXResult((org.xml.sax.ContentHandler) hs);

 
xmlTransformer.transform(xmlSource, xlsResult);

                                }

                                catch (Exception ex)

                                {

 
logger.fatal(ex.getMessage());

                                                ex.printStackTrace();

                                } finally {

                                                hs = null;

//                                              os = null;

                                                xmlFactory = null;

                                                xmlTransformer = null;

                                                xmlSource = null;

                                                xlsResult = null;

                                }

    }

 

    public static void main(String[] args) throws FileNotFoundException,
Exception {

                                org.apache.log4j.PatternLayout loggerLayout
= null;

                                org.apache.log4j.WriterAppender
loggerAppender = null;

                                java.io.FileOutputStream loggerOutput =
null;

 

                                try {

                                                loggerOutput = new
java.io.FileOutputStream("C:\\eclipse-SDK\\eclipse-3.2.2\\workspace\\Testing
\\WEB-INF\\classes\\XML2XLSTransfomer.log");

                                                loggerLayout = new
org.apache.log4j.PatternLayout();

                                                loggerAppender = new
org.apache.log4j.WriterAppender(loggerLayout, loggerOutput);

 
logger.addAppender(loggerAppender);

 
logger.setLevel(org.apache.log4j.Level.INFO);

 

 
logger.info("XML2XLSTransfomer::START");

 

                                                new
XML2XLSTransformer().convert(new FileInputStream(args[0]),new
FileOutputStream(args[1]));

 

 
logger.info("XML2XLSTransfomer::END");

                                } catch(Exception ex) {

 
logger.fatal(ex.getMessage());

                                                ex.printStackTrace();

                                }

                }

}

 

 

Thanks & Regards

 




Chan Mei Theng
Technical Developer


m: +6012 488 4484


t: +603 2163 7233


f: +603 2163 8233


 <mailto:meitheng@pocketgroup.co.uk> meitheng@pocketgroup.co.uk


 <http://www.pocketgroup.co.uk/> www.pocketgroup.co.uk


skype ID: mtchan48

 





Experts in mobile content and services 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Mime
View raw message