XMLMill - convert xml to pdf with java. Generate PDF from xml/xsl.

XMLMill User Guide

Version: 3.00 Date: January 2nd, 2008
This tutorial is opened in a separate window in order to maximize the legibility of this tutorial.
To return to XMLMill, close this browser window

This page as PDFPrinter friendly pageThis guide (!) as PDF

XMLMill and DOM

If the xml and/or the xsl data is contained in a org.w3c.dom.Document instance, these instances can be passed to the JAXPTransformer instance using following methods:

setXMLDocument(org.w3c.dom.Document)

Use this method to pass the xml data as a org.w3c.dom.Document instance.

setXSLDocument(org.w3c.dom.Document)

Use this method to pass the xsl data as a org.w3c.dom.Document instance.

  • It is not necessary that both xml and xsl data are both dom documents.

Example 1: TestDOMXML

In this example the xml data is contained in a org.w3c.dom.Document document instance, the xsl data is contained in a file on disk.

[001] 
[002] package com.xmlmilltest;
[003] 
[004] import com.xmlmill.*;
[005] import com.xmlmill.config.Configurator;
[006] 
[007] // Imported DOM classes
[008] import org.w3c.dom.Document;  
[009] 
[010] // Imported JAVA API for XML Parsing classes
[011] import javax.xml.parsers.DocumentBuilder;
[012] import javax.xml.parsers.DocumentBuilderFactory;
[013] import javax.xml.parsers.ParserConfigurationException; 
[014] 
[015] import java.io.File;
[016] 
[017] public class TestDOMXML {
[018]   
[019]   /** Creates a new instance of TestDOM */
[020]   public TestDOMXML() {
[021]     //
[022]   }
[023]         
[024]   public static void main(String[] args) 
[025]   throws Throwable {
[026]     JAXPTransformer transform = null;
[027]     File logfile              = null;
[028]     Document xmldoc           = null;
[029]     FileLogger logger         = null; 
[030]       try {       
[031]       
[032]       
[033]         ////////////////////////////////////////////////////
[034]         // DEFINE FIRST THE LOGGER INSTANCE TO USE        //
[035]         // HERE DEFAULT com.xmlmill.log.FileLogger        //
[036]         // (you can use any class which implements the    //
[037]         // org.apache.commons.logging.Log interface)      //
[038]         ////////////////////////////////////////////////////
[039] 
[040]         // Use the default FileLogger class
[041]         logger = new FileLogger("xmlmill");
[042]  
[043]         // Set logfile
[044]         logger.setFileName("file:/C:/xmlmill3.00/testtransform.txt", 
              false);
[046]       
[047]         // Set the loglevel 
[048]         logger.setLevel("INFO");
[049] 
[050]         // Pass the logger to the XMLMill Configurator, so XMLMill can use 
              it.
[052]         Configurator.setLogger(logger);
[053]     
[054]         //////////////////////////////////////////////
[055]         // INITIALIZE THE CONFIGURATOR              //
[056]         //////////////////////////////////////////////    
[057]          
[058]         // Define a configurator (optional)
[059]         Configurator.configure();
[060]       
[061]         //////////////////////////////////////////////
[062]         // CONTINUE                                 //
[063]         //////////////////////////////////////////////      
[064]        
[065]         // --------------------------------------
[066]         // Part I : 1. Create a XML DomDocument -   
[067]         // --------------------------------------
[068]         //Instantiate a DocumentBuilderFactory.
[069]         DocumentBuilderFactory dFactory = 
              DocumentBuilderFactory.newInstance();
[071] 
[072]         //Use the DocumentBuilderFactory to create a DocumentBuilder.
[073]         DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
[074]       
[075]         //Use the DocumentBuilder to parse the XML input.
[076]         xmldoc = 
              dBuilder.parse("file:/C:/XMLMill/examples/phonelist.xml");  
[078]         
[079]         // ---------------------------------------------
[080]         // Part II : 1. Create a JAXPTransformer  instance -
[081]         //          2. Set corresponding methods       -  
[082]         // ---------------------------------------------
[083]        
[084]         // Define a tranform object 
[085]         transform = new JAXPTransformer();
[086]         
[087]         // Define an errorhandler(optional) »
[088]         JAXPErrorHandler ehandler = new JAXPErrorHandler(
[089]                  JAXPErrorHandler.ABORT_ON_ERROR );
[090]                  
[091]         // Set the xml DomDocument file
[092]         transformer.setXMLDocument(xmldoc);
[093]       
[094]         // Set the xsl file
[095]         transform.setXSLDocument(new 
              File("C:\\XMLMill\\examples\\phonelist.xsl")); 
[097] 
[098]         // Sets the URL against which relative URLs in the result-tree 
              will be resolved.
[100]         transformer.setBaseURL("file:/C:/XMLMill/examples");               
                
[102] 
[103] 
[104]         // Set the errorhandler
[105]         transform.setErrorHandler(ehandler);
[106]         
[107]         // Set the output folder
[108]         transformer.setOutputFolder(new 
              File("C:\\XMLMill3.00\\samples\\xmlxsl\\dom\\"));
[110]         
[111]         // --------------------------------------
[112]         //  Part III:  1. Do the transformation -        
[113]         // --------------------------------------
[114]         transform .transform();   
[115]      
[116]      } finally {   
[117]         // Close the logger instance
[118]         logger.finalize();
[119]     
[120]         // Null out instances
[121]         transform = null;
[122]         logger    = null;
[123]         xmldoc    = null;        
[124]         System.out.println("-- Done --");  
[125]       }     
[126]    }    
[127] }           
[128]       

1

Import the needed classes for setting up a org.w3c.dom.Document document.

2

The xml data is contained in a org.w3c.dom.Document Document (build from a file on the file-system).

3

Note that the xsl document is referenced using a File instance.

4

The transformation is done.

5

Dereference any objects.

Example2: TestDOMXMLXSL

In the next example the xml and xsl data are both org.w3c.dom.Document documents.

[001] 
[002] package com.xmlmilltest;
[003] 
[004] import com.xmlmill.*;
[005] import com.xmlmill.config.Configurator;
[006] 
[007] // Imported DOM classes
[008] import org.w3c.dom.Document;     
[009] 
[010] // Imported JAVA API for XML Parsing classes
[011] import javax.xml.parsers.DocumentBuilder;
[012] import javax.xml.parsers.DocumentBuilderFactory;
[013] import javax.xml.parsers.ParserConfigurationException; 
[014] 
[015] import java.io.File;
[016] 
[017] public class TestDOMXMLXSL {
[018]   
[019]   /** Creates a new instance of TestDOM */
[020]   public TestDOMXMLXSL() {
[021]     //
[022]   }
[023]         
[024]   public static void main(String[] args) 
[025]   throws Throwable {
[026]     JAXPTransformer transform = null;
[027]     File logfile              = null;
[028]     Document xmldoc           = null;
[029]     Document xsldoc           = null;    
[030]     FileLogger logger         = null;
[031]       
[032]       try {       
[033]       
[034]         ////////////////////////////////////////////////////
[035]         // DEFINE FIRST THE LOGGER INSTANCE TO USE        //
[036]         // HERE DEFAULT com.xmlmill.log.FileLogger        //
[037]         // (you can use any class which implements the    //
[038]         // org.apache.commons.logging.Log interface)      //
[039]         ////////////////////////////////////////////////////
[040] 
[041]         // Use the default FileLogger class
[042]         logger = new FileLogger("xmlmill");
[043] 
[044]         // Set logfile
[045]         logger.setFileName("file:/C:/xmlmill3.00/testtransform.txt", 
              false);
[047]       
[048]         // Set the loglevel 
[049]         logger.setLevel("INFO");
[050] 
[051]         // Pass the logger to the XMLMill Configurator, so XMLMill can use 
              it.
[053]         Configurator.setLogger(logger);
[054]     
[055]         //////////////////////////////////////////////
[056]         // INITIALIZE THE CONFIGURATOR              //
[057]         //////////////////////////////////////////////    
[058]          
[059]         // Define a configurator (optional)
[060]         Configurator.configure();
[061]       
[062]         //////////////////////////////////////////////
[063]         // CONTINUE                                 //
[064]         //////////////////////////////////////////////      
[065]        
[066]         // --------------------------------------
[067]         // Part I : 1. Create a XML DomDocument -   
[068]         // --------------------------------------
[069]         
[070]         //Instantiate a DocumentBuilderFactory.
[071]         DocumentBuilderFactory dFactory = 
              DocumentBuilderFactory.newInstance();
[073] 
[074]         //Use the DocumentBuilderFactory to create a DocumentBuilder.
[075]         DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
[076]       
[077]         //Use the DocumentBuilder to parse the XML input.
[078]         xmldoc = 
              dBuilder.parse("file:/C:/XMLMill/examples/phonelist.xml");  
[080] 
[081]         // ---------------------------------------
[082]         // Part II : 2. Create a XSL DomDocument -  
[083]         // ---------------------------------------
[084] 
[085]         // setNamespaceAware, which is required when parsing xsl files
[086]         dFactory.setNamespaceAware(true);        
[087]         
[088]         //Use the DocumentBuilderFactory to create a DocumentBuilder.
[089]         dBuilder = dFactory.newDocumentBuilder();
[090]       
[091]         //Use the DocumentBuilder to parse the XML input.
[092]         xsldoc = 
              dBuilder.parse("file:/C:/XMLMill/examples/phonelist.xsl");  
[094]                 
[095]         // ---------------------------------------------   
[096]         // Part II : 1. Create a JAXPTransformer  instance - 
[097]         //           2. Set corresponding methods      -  
[098]         // ---------------------------------------------
[099]         
[100]         // Define a tranform object 
[101]         transform = new JAXPTransformer();
[102]                
[103]         // Define an errorhandler(optional) »
[104]         JAXPErrorHandler ehandler = new JAXPErrorHandler(
[105]                 JAXPErrorHandler.ABORT_ON_ERROR );
[106] 
[107]         // Set the xml DomDocument file
[108]         transform.setXMLDocument(xmldoc);
[109] 
[110]         // Set the xsl DomDocument file
[111]         transform.setXSLDocument(xsldoc);
[112]         
[113]         // Sets the URL against which relative URLs in the result-tree 
              will be resolved.
[115]         transform.setBaseURL("file:/C:/XMLMill/examples/phonelist.xsl");
[116] 
[117]         // Set the errorhandler
[118]         transform.setErrorHandler(ehandler);
[119]         
[120]         // Set the output folder
[121]         transformer.setOutputFolder(new 
              File("C:\\XMLMill3.00\\samples\\xmlxsl\\dom\\"));
[123]         
[124]         // --------------------------------------
[125]         //  Part III:  1. Do the transformation -         
[126]         // --------------------------------------
[127]         transform .transform();          
[128]      
[129]      } finally { 
[130]        // Close the logger instance
[131]        logger.finalize();
[132]     
[133]        // Null out instances
[134]        transform = null;
[135]        logger    = null;
[136]        xmldoc    = null;       
[137]        System.out.println("-- Done --");    
[138]      }     
[139]      
[140]      
[141]    }    
[142] }  
1

Import the needed classes for setting up a org.w3c.dom.Document document.

2

The xml data is contained in a org.w3c.dom.Document Document (build from a file on the file-system).

3

The xsl data is contained in a org.w3c.dom.Document Document (build from a file on the file-system).

4

Create a JAXPTransformer object and set corresponding methods.

5

The transformation is done.

6

The objects are dereferenced.

  • XMLMill uses a org.apache.xml.serialize.XMLSerializer instance to convert the org.w3c.dom.Document instance(s) to as SAX java.io.InputStream instance(s), using separate Threads.
Copyright © 2001 - 2012. All rights reserved. XMLMill and XMLMill logo are trademarks of Pecunia Data Systems, bvba.
Powered by Apache CocoonPowered by XMLMill