Example usage for javax.xml.transform Transformer transform

List of usage examples for javax.xml.transform Transformer transform

Introduction

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

Prototype

public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;

Source Link

Document

Transform the XML Source to a Result.

Usage

From source file:Main.java

/**
 * Saves the XML file to disk./*from w w  w.  j  av  a  2s.  c  o m*/
 * @param fileName The name of the file.
 * @param doc The XML Document to save to disk.
 * @throws TransformerConfigurationException
 * @throws TransformerException 
 */
private static void store(String fileName, Document doc)
        throws TransformerConfigurationException, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    transformer.transform(new DOMSource(doc), new StreamResult(new File(fileName)));
}

From source file:Main.java

private static void WriteXMLFile(Document doc, OutputStreamWriter w, String encoding) {
    try {/*from  w w  w  .j  a v  a  2  s .  c  o m*/
        Source source = new DOMSource(doc);
        Result ret = new StreamResult(w);
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        xformer.transform(source, ret);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();

    }

}

From source file:Main.java

private static void wirteXmlFile(Document doc, File file) throws TransformerException {
    // Write the content into xml file
    //logger.debug("Changes are done in xml file, Writing to the XML file");
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(file);
    transformer.transform(source, result);
}

From source file:Main.java

public static String nodeToString(Node noh) throws TransformerException {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    StringWriter wr = new StringWriter();
    StreamResult result = new StreamResult(wr);

    t.transform(new DOMSource(noh), result);
    return wr.toString();
}

From source file:Main.java

public static void transfor(Document doc, OutputStream out) {
    try {//from   w  w  w. j  a  va  2  s.c  om
        Transformer tf = tff.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(out);
        tf.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static String prettyFormat(String input, int indent) {
    try {//  w w w .  j  a va 2  s.  c  o m
        Source xmlInput = new StreamSource(new StringReader(input));
        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

public static void transfer(Document doc, String filepath) {
    try {//from   w ww  . ja v a2s  .c  o  m
        Transformer tf = tff.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(filepath);
        tf.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static String transform(Source xsltStream, Source xmlStream, String charset)
        throws TransformerException {
    try {/*from w ww.  j ava  2 s  .c o m*/

        Transformer transformer = factory.newTransformer(xsltStream);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        transformer.transform(xmlStream, new StreamResult(outputStream));
        transformer = null;
        xmlStream = null;
        return outputStream.toString(charset);
    } catch (Exception e) {
        throw new TransformerException(e);
    }
}

From source file:Main.java

public static void writeDocument(Document document, OutputStream out) throws TransformerException {
    TransformerFactory transformFactory = TransformerFactory.newInstance();
    Transformer idTransform = transformFactory.newTransformer();
    Source input = new DOMSource(document);
    Result output = new StreamResult(out);
    idTransform.transform(input, output);
}

From source file:Main.java

public static String toString(Node node) {
    DOMSource domSource = new DOMSource(node);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    try {//w  w  w . j  a  v  a 2s .com
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}