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 String format(String input, int indent) {
    try {// w  w w . j a  v  a2s  .  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);
    }
}

From source file:Main.java

public static void transform(InputStream xslis, InputStream xmlis, OutputStream xmlos) {
    try {//from   w w  w.  ja v  a  2s.  c  o m
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xslis));
        transformer.transform(new StreamSource(xmlis), new StreamResult(xmlos));
        xslis.close();
        xmlis.close();
        xmlos.close();
    } catch (Exception e) {
        throw new RuntimeException("Fail to do XSLT transformation", e);
    }
}

From source file:Main.java

public static String format(Node node) throws TransformerException {
    final StringWriter writer = new StringWriter();
    format(new DOMSource(node), false, new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static String xmlDocumentToString(final Document doc) {
    try {/*from  ww  w .  ja v a 2s  . c om*/
        final DOMSource domSource = new DOMSource(doc);
        final StringWriter writer = new StringWriter();
        final StreamResult result = new StreamResult(writer);
        final TransformerFactory tf = TransformerFactory.newInstance();
        final Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (final TransformerException ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void saveXML(Document doc, OutputStream stream) {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer;/*from w  w  w.  j  a  v a2 s  .  c  o m*/
    try {
        transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(stream);
        transformer.transform(source, result);
        stream.flush();
        stream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static synchronized String formatXml(String xml) {

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {/*w  ww . j  a  va 2 s  . c om*/
        readableXformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(output));
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to format XML", e);
    }
    return output.toString();

}

From source file:Main.java

public static void serialize(Document document, File targetFile) throws IOException {
    try {/*from  www .j  av  a2  s .co m*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.transform(new DOMSource(document), new StreamResult(targetFile));
    } catch (TransformerException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static String NodeToStr(Node node) throws Exception, TransformerFactoryConfigurationError {
    StringWriter sw = new StringWriter();
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static String xmlParaString(Node doc) {
    DOMSource xmlSource = new DOMSource(doc);

    StringWriter sw = new StringWriter();
    Result result = new StreamResult(sw);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer;//ww w .j  av a  2s. c  om

    try {
        transformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }

    try {
        transformer.transform(xmlSource, result);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return sw.toString();
}

From source file:Main.java

public static void write2Xml(Document document) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer tf = factory.newTransformer();
    tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(fileName)));
}