Example usage for javax.xml.transform TransformerFactory newTransformer

List of usage examples for javax.xml.transform TransformerFactory newTransformer

Introduction

In this page you can find the example usage for javax.xml.transform TransformerFactory newTransformer.

Prototype

public abstract Transformer newTransformer(Source source) throws TransformerConfigurationException;

Source Link

Document

Process the Source into a Transformer Object .

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    StreamSource source = new StreamSource(args[0]);
    StreamSource stylesource = new StreamSource(args[1]);

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(stylesource);

    StreamResult result = new StreamResult(System.out);
    transformer.transform(source, result);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    StreamSource xmlFile = new StreamSource(new File(args[0]));
    StreamSource xsltFile = new StreamSource(new File(args[1]));
    TransformerFactory xsltFactory = TransformerFactory.newInstance();
    Transformer transformer = xsltFactory.newTransformer(xsltFile);

    StreamResult resultStream = new StreamResult(System.out);
    transformer.transform(xmlFile, resultStream);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL webSvcGetURL = new URL("http://www.server.net/Webservices");
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(webSvcGetURL.openStream()));
    SAXSource saxSource = new SAXSource(new InputSource(bufferedReader));
    String curDir = new File(".").getCanonicalPath();
    StreamSource xlstStreamSource = new StreamSource(new File(curDir + File.separator + "style.xsl"));
    File resultHTMLFile = new File(curDir + File.separator + "output.html");

    StreamResult streamResult = new StreamResult(resultHTMLFile);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(xlstStreamSource);
    transformer.transform((Source) saxSource, streamResult);
}

From source file:XSLTransform.java

public static void main(String[] args) throws TransformerException {
    // Set up streams for input, stylesheet, and output.
    // These do not have to come from or go to files. We can also use the
    // javax.xml.transform.{dom,sax} packages use DOM trees and streams of
    // SAX events as sources and sinks for documents and stylesheets.
    StreamSource input = new StreamSource(new File(args[0]));
    StreamSource stylesheet = new StreamSource(new File(args[1]));
    StreamResult output = new StreamResult(new File(args[2]));

    // Get a factory object, create a Transformer from it, and
    // transform the input document to the output document.
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(stylesheet);
    transformer.transform(input, output);
}

From source file:XMLTransform.java

public static void main(String[] args) throws Exception {
    System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    XMLReader reader = parser.getXMLReader();
    TransformerFactory factory = TransformerFactory.newInstance();
    System.out.println(factory);//from w w w . ja va2s .com
    Transformer transformer = factory.newTransformer(new StreamSource("./xsl/books-sql.xsl"));
    transformer.setParameter("user", "root");
    transformer.setParameter("password", "123456");
    transformer.transform(new StreamSource("./xml/books.xml"), new StreamResult(System.out));
}

From source file:UseStylesheetParam.java

public static void main(String[] args)
        throws TransformerException, TransformerConfigurationException, SAXException, IOException {
    if (args.length != 1) {
        System.err.println("Please pass one string to this program");
        return;/*from w w  w  .  ja v a2 s. c o  m*/
    }
    // Get the parameter value from the command line.
    String paramValue = args[0];

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource("foo.xsl"));

    // Set the parameter. I can't get non-null namespaces to work!!
    transformer.setParameter("param1", /* parameter name */
            paramValue /* parameter value */ );

    transformer.transform(new StreamSource("foo.xml"), new StreamResult(new OutputStreamWriter(System.out)));
}

From source file:JAXPTransletOneTransformation.java

public static void main(String argv[]) throws TransformerException, TransformerConfigurationException,
        IOException, SAXException, ParserConfigurationException, FileNotFoundException {
    // Set the TransformerFactory system property to generate and use a translet.
    // Note: To make this sample more flexible, load properties from a properties file.    
    // The setting for the Xalan Transformer is "org.apache.xalan.processor.TransformerFactoryImpl"
    String key = "javax.xml.transform.TransformerFactory";
    String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl";
    Properties props = System.getProperties();
    props.put(key, value);//from w  w w.jav a 2s.c  o  m
    System.setProperties(props);

    String xslInURI = "todo.xsl";
    String xmlInURI = "todo.xml";
    String htmlOutURI = "todo.html";
    try {
        // Instantiate the TransformerFactory, and use it along with a SteamSource
        // XSL stylesheet to create a Transformer.
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(new StreamSource(xslInURI));
        // Perform the transformation from a StreamSource to a StreamResult;
        transformer.transform(new StreamSource(xmlInURI), new StreamResult(new FileOutputStream(htmlOutURI)));
        System.out.println("Produced todo.html");
    } catch (Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }
}

From source file:XMLTransform.java

public static void main(String args[]) {

    if (args.length != 2) {
        System.err.println("Usage: java XMLTransform xmlfile.xml stylesheet.xsl");
        System.exit(-1);//from w  w w .j av a2 s  .com
    }

    try {
        StreamSource source = new StreamSource(args[0]);
        StreamSource stylesource = new StreamSource(args[1]);

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(stylesource);

        StreamResult result = new StreamResult(System.out);
        transformer.transform(source, result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

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

    String xmlSample = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><tag><nested>hello</nested></tag>";
    String stylesheet = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output method=\"xml\" version=\"1.0\" indent=\"yes\"/><xsl:template match=\"node()|@*\"><xsl:copy><xsl:apply-templates select=\"node()|@*\"/></xsl:copy></xsl:template></xsl:stylesheet>";

    TransformerFactory factory = TransformerFactory.newInstance();

    Source xslSource = new StreamSource(new StringReader(stylesheet));
    Transformer transformer = factory.newTransformer(xslSource);

    Source source = new StreamSource(new StringReader(xmlSample));
    Result result = new StreamResult(System.out);

    transformer.transform(source, result);

}

From source file:Trace.java

public static void main(String[] args) throws java.io.IOException, TransformerException,
        TransformerConfigurationException, java.util.TooManyListenersException, org.xml.sax.SAXException {
    String fileName = "foo";
    if (args.length > 0)
        fileName = args[0];/*from  w w w  . j  a  v  a 2 s .  co  m*/

    // Set up a PrintTraceListener object to print to a file.
    java.io.FileWriter fw = new java.io.FileWriter("events.log");
    java.io.PrintWriter pw = new java.io.PrintWriter(fw, true);
    PrintTraceListener ptl = new PrintTraceListener(pw);

    // Print information as each node is 'executed' in the stylesheet.
    ptl.m_traceElements = true;
    // Print information after each result-tree generation event.
    ptl.m_traceGeneration = true;
    // Print information after each selection event.
    ptl.m_traceSelection = true;
    // Print information whenever a template is invoked.
    ptl.m_traceTemplates = true;
    // Print information whenever an extension call is made.
    ptl.m_traceExtension = true;

    // Set up the transformation    
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource(fileName + ".xsl"));

    // Cast the Transformer object to TransformerImpl.
    if (transformer instanceof TransformerImpl) {
        TransformerImpl transformerImpl = (TransformerImpl) transformer;
        // Register the TraceListener with a TraceManager associated 
        // with the TransformerImpl.
        TraceManager trMgr = transformerImpl.getTraceManager();
        trMgr.addTraceListener(ptl);

        // Perform the transformation --printing information to
        // the events log during the process.
        transformer.transform(new StreamSource(fileName + ".xml"),
                new StreamResult(new java.io.FileWriter(fileName + ".out")));
    }
    // Close the PrintWriter and FileWriter.
    pw.close();
    fw.close();
    System.out.println("**The output is in " + fileName + ".out; the log is in events.log ****");

}