Example usage for javax.xml.transform.stream StreamResult StreamResult

List of usage examples for javax.xml.transform.stream StreamResult StreamResult

Introduction

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

Prototype

public StreamResult(File f) 

Source Link

Document

Construct a StreamResult from a File.

Usage

From source file:Main.java

public static void printDocument(Document doc, OutputStream out) {
    try {//  www.j  a v a2  s  .  co  m
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
    } catch (Exception e) {
        System.out.println("Error while printing the document.");
    }
}

From source file:Main.java

public static void printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;/*from  w ww.j av  a  2 s .  c  o m*/
    try {
        serializer = tfactory.newTransformer();
        if (prettyPrint) {
            //Setup indenting to "pretty print"
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        }
        if (!includeXmlDeclaration) {
            serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }

        DOMSource xmlSource = new DOMSource(node);
        StreamResult outputTarget = new StreamResult(out);
        serializer.transform(xmlSource, outputTarget);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void transfer(Document doc, OutputStream result)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {

    transfer(doc, new StreamResult(result));
}

From source file:Main.java

public static void createEmptyXML222(String xmlFile) {
    try {//from w w w  . j  a  va  2 s. c  om
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        FileOutputStream fo = new FileOutputStream(xmlFile);
        StreamResult result = new StreamResult(fo);
        transformer.transform(source, result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void toWriter(Document doc, Writer writer) {
    if (doc == null || writer == null) {
        return;//  w  w w  .  j a va  2  s  .  c o  m
    }
    try {
        Transformer tran = tf.newTransformer();
        tran.setOutputProperty(OutputKeys.INDENT, "yes");
        Source src = new DOMSource(doc);
        Result res = new StreamResult(writer);
        tran.transform(src, res);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Transforms an XML to a String// w w  w  .j a v  a2  s . c o  m
 * @param node XML node
 * @return String represenation of XML
 */
public static String printXML(Node node) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;
    try {
        serializer = tfactory.newTransformer();

        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        StringWriter output = new StringWriter();
        serializer.transform(new DOMSource(node), new StreamResult(output));
        return output.toString();
    } catch (TransformerException e) {

        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * @param xml/*w  w w . ja  v  a  2 s  .  com*/
 * @return pretty xml
 */
public static String prettyXml(String xml) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;
    try {
        Source source = new StreamSource(new StringReader(xml));
        StringWriter writer = new StringWriter();

        serializer = tfactory.newTransformer();
        // Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        serializer.transform(source, new StreamResult(writer));
        return writer.toString();
    } catch (Exception e) {
        return xml;
    }
}

From source file:Main.java

public static String xmlToString(Node doc) {
    try {/*from w w  w .j a v a 2s.co m*/
        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.transform(domSource, result);
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static void overwriteXmlFile(File xmlFile, Document document, Transformer transformer)
        throws FileNotFoundException, TransformerException {
    StreamResult result = new StreamResult(new PrintWriter(new FileOutputStream(xmlFile, false)));
    DOMSource source = new DOMSource(document);
    transformer.transform(source, result);
}

From source file:Main.java

public static void xsl(String inFilename, String outFilename, String xslFilename) {
    try {/*from  ww w . ja v  a 2  s  .  co  m*/
        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();
    }
}