/** * This class applies the XSL stylesheet provided as the 2nd argument * to the XML document provided as the 1st argument. The result tree * is shown on System.out * Argument 1: XML document to transform * Argument 2: XSL style sheet to apply to the XML document * @author Nauman Chaudhry * @date 04/16/07 */ import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import org.w3c.dom.*; import java.io.*; public class TransformDocument { public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; try { db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException pce) { pce.printStackTrace(); return; } // Open the XML document using DOM parser. Document doc = db.parse(args[0]); // Create a transformer factory TransformerFactory tf = TransformerFactory.newInstance(); // Create a transformer and associate the stylesheet with the transformer. Transformer trans = tf.newTransformer(new StreamSource(args[1])); // Apply the stylesheet to the source document and print out the // result tree on standard output trans.transform(new DOMSource(doc), new StreamResult(System.out)); } }