The Java Program: Transform.java

  1 import javax.xml.parsers.*;  // j2se 1.4
  2 
  3 
  4 import javax.xml.transform.Templates;
  5 import javax.xml.transform.Transformer;
  6 import javax.xml.transform.stream.StreamSource;
  7 import javax.xml.transform.stream.StreamResult;
  8 import javax.xml.transform.TransformerException;
  9 import javax.xml.transform.TransformerFactory;
 10 
 11 import java.io.*;
 12 
 13 public class Transform {
 14 
 15    public static void main (String[] args) throws Exception {
 16       final String xml_url = args[0];
 17       final String xsl_filename = args[1];
 18       final TransformerFactory tfactory = TransformerFactory.newInstance();
 19                         
 20       try {
 21          final InputStream xslIS = new FileInputStream (xsl_filename);
 22          final StreamSource xslSource = new StreamSource (xslIS);
 23          // pre-compile the stylesheet
 24          final Templates templates = tfactory.newTemplates (xslSource);
 25                         
 26          //      final StringWriter sos = new StringWriter();
 27          final StreamResult out = new StreamResult (System.out);
 28                             
 29          final Transformer transformer = templates.newTransformer();
 30 
 31          final StreamSource xmlSource = new StreamSource (xml_url);
 32          
 33          transformer.transform (xmlSource, out);
 34          // sos.close();
 35          //      final String new_xml = sos.toString();
 36          //System.out.print (new_xml);
 37       } catch (IOException e) {
 38          System.err.println (e);
 39       }
 40    }
 41 
 42 }