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

/**
 * //from  ww w .j a  v a  2s. com
 * @param xml
 * @param indent
 * @return pretty formatted xml
 */
public static String prettyFormat(String xml, int indent) {
    try {
        Source xmlInput = new StreamSource(new StringReader(xml));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:Main.java

/**
 * Writes an XML element to a given file
 * @param doc XML element//from  w ww  .j a va 2  s.  c  o m
 * @param filename Filename of the file where to write XML
 */
public static void writeXmlFile(Element doc, String filename) {
    try {

        Source source = new DOMSource(doc);
        File file = new File(filename);
        Result result = new StreamResult(file);

        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void saveReportToFile(Node node, String documentFileName) {
    // StringWriter writer = new StringWriter();
    // StreamResult resultString = new StreamResult(writer);

    File output = new File(documentFileName);
    StreamResult resultFile = new StreamResult(output);

    DOMSource source = new DOMSource(node);
    try {//w  w  w .  jav  a  2 s  . c  om
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        t.transform(source, resultFile);
        // t.transform(source, resultString);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    // return writer.toString();
}

From source file:Main.java

public static String writeDocumentToString(Document document) {
    try {/*w ww  . j ava  2  s .  c om*/
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans;
        trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        StringWriter writer = new StringWriter();
        try {
            StreamResult streamResult = new StreamResult(writer);
            DOMSource source = new DOMSource(document);
            trans.transform(source, streamResult);
        } catch (TransformerException e) {
            e.printStackTrace();
            return null;
        } finally {
            writer.close();
        }
        return writer.toString();

    } catch (TransformerConfigurationException | IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

/**
 * Get DOM as a string/*from  w  w  w.j a v a2  s  .  c o  m*/
 * @param doc
 * @return
 */
public static String getStringFromDoc(org.w3c.dom.Document doc) {
    if (doc == null) {
        System.out.println("XML document is null");
    }
    try {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                "http://commons.omniupdate.com/dtd/standard.dtd");
        transformer.transform(domSource, result);
        writer.flush();
        return writer.toString();
    } catch (TransformerException ex) {
        System.out.println("Transformer Exception");
        // ex.printStackTrace();
        return "Error in transformation";
    }
}

From source file:Main.java

public static Document xslt(InputStream stylesheet, Document input)
        throws FileNotFoundException, TransformerException, ParserConfigurationException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource(stylesheet));
    Document result = newDocument();
    DOMResult domResult = new DOMResult(result);
    transformer.transform(new DOMSource(input), domResult);
    return result;
}

From source file:Main.java

private static String toString(final Document document, final boolean indent, final boolean omitXmlDeclaration)
        throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
    final Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
    transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    final StringWriter stringWriter = new StringWriter();
    final StreamResult streamResult = new StreamResult(stringWriter);

    transformer.transform(new DOMSource(document), streamResult);

    return stringWriter.toString();
}

From source file:Main.java

/**
 * Transforms a DOM node to a String./*ww  w  .ja  va2 s  .c  om*/
 *
 * @param node the node
 * @return the transformed XML representation
 * @throws TransformerException if a transformation error occurs.
 */
public static String transform(Node node) throws TransformerException {
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    Source source = new DOMSource(node);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(source, result);
    return writer.toString();
}

From source file:Main.java

public static InputStream newInputStreamFromDocument(Document doc)
        throws TransformerConfigurationException, TransformerException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Source xmlSource = new DOMSource(doc);
    Result outputTarget = new StreamResult(outputStream);
    TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
    return new ByteArrayInputStream(outputStream.toByteArray());
}

From source file:Main.java

public static ByteArrayOutputStream printDOMDocumentToOutputStream(Document doc) {
    ByteArrayOutputStream os;//from ww  w .j  a va 2s  .c  o  m
    os = new ByteArrayOutputStream();

    try {
        // TODO remove, nonsence here XMLSignatureHelper.storeSignatureToXMLFile(doc, "signature.xml");

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        //trans.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        //trans.setOutputProperty(OutputKeys.INDENT, "yes");
        //trans.setOutputProperty(OutputKeys.METHOD, "xml");

        trans.transform(new DOMSource(doc), new StreamResult(os));

    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return os;

}