Example usage for javax.xml.transform Templates newTransformer

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

Introduction

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

Prototype

Transformer newTransformer() throws TransformerConfigurationException;

Source Link

Document

Create a new transformation context for this Templates object.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Templates template = factory.newTemplates(new StreamSource(new FileInputStream("xsl.xlt")));
    Transformer xformer = template.newTransformer();
    Source source = new StreamSource(new FileInputStream("in.xml"));
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.newDocument();
    Result result = new DOMResult(doc);
    xformer.transform(source, result);/*  ww w . j av  a 2 s .c om*/
}

From source file:MainClass.java

public static void main(String argv[]) throws Exception {
    Properties props = System.getProperties();
    props.put("javax.xml.transform.TransformerFactory", "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");
    System.setProperties(props);/*from  www  .j  a  v a2  s  . c  o  m*/
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Templates translet = tFactory.newTemplates(new StreamSource("OrderProcessing.xslt"));

    Transformer transformer = translet.newTransformer();

    transformer.transform(new StreamSource("CustomerOrders.xml"),
            new StreamResult(new FileOutputStream("SortedOrders.html")));

    transformer.transform(new StreamSource("CustomerOrders1.xml"),
            new StreamResult(new FileOutputStream("SortedOrders1.html")));

}

From source file:Main.java

private static Transformer newTransformer(Document source) {
    Source tSource = new DOMSource(source, "");
    try {//from  www .  ja va  2s  . co m
        Templates tmpls = tFactory.newTemplates(tSource);
        Transformer ret = tmpls.newTransformer();
        return ret;
    } catch (TransformerConfigurationException e) {
        return null;
    }
}

From source file:Main.java

public static Node trance(Node context, String xpath) throws Exception {
    Templates txp = TrAXPath(xpath);
    return trance(context, txp.newTransformer());
}

From source file:Main.java

public static NodeList xPath(Node context, String xpath) throws Exception {
    Templates txp = TrAXPath(xpath);
    return selectNodeList(context, txp.newTransformer());
}

From source file:Main.java

/**
 * Perform an xsl transformation//from www.  ja v  a2  s.c o  m
 */
public static void transform(Source xsl, Source xml, Result result) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Templates template = factory.newTemplates(xsl);
    Transformer transformer = template.newTransformer();
    transformer.transform(xml, result);
}

From source file:Main.java

/**
 * @param xmlSource//from w w  w .jav a2s . c om
 * @param xslSource
 * @param resolver
 * @param output
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
public static void formatXML(Source xmlSource, Source xslSource, URIResolver resolver, Result result)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Templates t = factory.newTemplates(xslSource);
    Transformer transformer = t.newTransformer();
    if (resolver != null) {
        transformer.setURIResolver(resolver);
    }
    transformer.transform(xmlSource, result);
}

From source file:Main.java

/**
 * @param xmlsource/*w w w .j a v a  2 s . co  m*/
 * @param xsltfile
 * @return
 * @throws javax.xml.transform.TransformerException
 */
public static String transform(String xmlsource, File xsltfile) throws TransformerException {

    Source xmlSource = new StreamSource(new StringReader(xmlsource));
    Source xsltSource = new StreamSource(xsltfile);
    StringWriter stringWriter = new StringWriter();

    TransformerFactory transFact = TransformerFactory.newInstance();
    Templates cachedXSLT = transFact.newTemplates(xsltSource);
    Transformer trans = cachedXSLT.newTransformer();
    trans.transform(xmlSource, new StreamResult(stringWriter));

    return stringWriter.toString();
}

From source file:Main.java

/**
 * @param xmlsource/*from   w  w  w.jav  a2  s. c om*/
 * @param xsltstream
 * @return
 * @throws javax.xml.transform.TransformerException
 */
public static String transform(String xmlsource, InputStream xsltstream) throws TransformerException {

    Source xmlSource = new StreamSource(new StringReader(xmlsource));
    Source xsltSource = new StreamSource(xsltstream);
    StringWriter stringWriter = new StringWriter();

    TransformerFactory transFact = TransformerFactory.newInstance();
    Templates cachedXSLT = transFact.newTemplates(xsltSource);
    Transformer trans = cachedXSLT.newTransformer();
    trans.transform(xmlSource, new StreamResult(stringWriter));

    return stringWriter.toString();
}

From source file:Main.java

public static void xsl(String inFilename, String outFilename, String xslFilename) {
    try {//from w w w . ja v  a2s  .  com
        TransformerFactory factory = TransformerFactory.newInstance();
        Templates template = factory.newTemplates(new StreamSource(new FileInputStream(xslFilename)));
        Transformer xformer = template.newTransformer();
        Source source = new StreamSource(new FileInputStream(inFilename));
        Result result = new StreamResult(new FileOutputStream(outFilename));
        xformer.transform(source, result);
    } catch (FileNotFoundException e) {
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
        SourceLocator locator = e.getLocator();
        int col = locator.getColumnNumber();
        int line = locator.getLineNumber();
        String publicId = locator.getPublicId();
        String systemId = locator.getSystemId();
    }
}