Example usage for javax.xml.transform TransformerFactory newInstance

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

Introduction

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

Prototype

public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError 

Source Link

Document

Obtain a new instance of a TransformerFactory .

Usage

From source file:Main.java

public static String transform(String xmlString, String stylesheetPathname) throws TransformerException {
    try {//from ww  w  .  j a v  a  2s  .c  o m
        TransformerFactory factory = TransformerFactory.newInstance();
        Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
        Transformer transformer = factory.newTransformer(stylesheetSource);
        Source inputSource = new StreamSource(new StringReader(xmlString));
        Writer outputWriter = new StringWriter();
        Result outputResult = new StreamResult(outputWriter);
        transformer.transform(inputSource, outputResult);
        return outputWriter.toString();
    } catch (TransformerConfigurationException tce) {
        throw new TransformerException(tce.getMessageAndLocation());
    } catch (TransformerException te) {
        throw new TransformerException(te.getMessageAndLocation());
    }
}

From source file:Main.java

public static synchronized Transformer getSyncTransformer() throws TransformerConfigurationException {
    TransformerFactory tf = TransformerFactory.newInstance();
    return tf.newTransformer();
}

From source file:Main.java

public static String documentToString(Document document) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/*w  ww. j a  va 2  s . c  o  m*/
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(document), new StreamResult(writer));
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return output;
}

From source file:Main.java

public static void createSVGFile(File file, Document document) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    Result svgOutput = new StreamResult(file);
    transformer.transform(new DOMSource(document), svgOutput);
}

From source file:Main.java

public static String getAsText(Node n) {
    try {// w ww.  j  ava2 s  .c  o  m
        Transformer t = TransformerFactory.newInstance().newTransformer();
        StringWriter out = new StringWriter();
        t.transform(new javax.xml.transform.dom.DOMSource(n), new javax.xml.transform.stream.StreamResult(out));
        return out.toString();
    } catch (Throwable t) {
        t.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String getXmlFileToString(String filePath) throws TransformerException {
    File xmlFile = new File(filePath);
    StringWriter writer = new StringWriter();
    TransformerFactory fac = TransformerFactory.newInstance();
    Transformer x = fac.newTransformer();
    x.transform(new StreamSource(xmlFile), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static String transform(String xmlString, String stylesheetPathname) throws Exception {
    try {/*  w w  w. ja v a2 s .  c  o m*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
        Transformer transformer = factory.newTransformer(stylesheetSource);
        Source inputSource = new StreamSource(new StringReader(xmlString));
        Writer outputWriter = new StringWriter();
        Result outputResult = new StreamResult(outputWriter);
        transformer.transform(inputSource, outputResult);
        return outputWriter.toString();
    } catch (TransformerConfigurationException tce) {
        throw new Exception(tce.getMessageAndLocation());
    } catch (TransformerException te) {
        throw new Exception(te.getMessageAndLocation());
    }
}

From source file:Main.java

private static void checkTransformFactory() throws TransformerConfigurationException {
    if (transformerFactory == null) {
        transformerFactory = TransformerFactory.newInstance();
    }/*from  w w w.  ja  v  a2s  . c  o  m*/
}

From source file:Main.java

public static void print(Node dom) {
    try {/*from  ww w  .  j  a  va  2 s.co m*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer t = factory.newTransformer();
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(dom), new StreamResult(sw));
        System.out.println(sw.toString());
    } catch (Exception ex) {
        System.out.println("Could not print XML document");
    }
}

From source file:Main.java

private static Transformer getTransformer() throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    return transformer;
}