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

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

Introduction

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

Prototype

public Writer getWriter() 

Source Link

Document

Get the character stream that was set with setWriter.

Usage

From source file:Main.java

private static String docToString(Document doc) throws Exception {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new StringWriter());
    transformer.transform(source, result);

    return result.getWriter().toString();
}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {/*w  w w. java  2 s . com*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        return result.getWriter().toString();
    } catch (TransformerException ex) {
        System.err.println("Error: Cannot load xml transformer");
        System.err.println("Cause: " + ex.toString());
        System.exit(1);
        return null;
    }
}

From source file:Main.java

private static final String transform(Transformer transformer, Node element) throws TransformerException {
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(element);
    transformer.transform(source, result);

    String xmlString = result.getWriter().toString();
    return xmlString;
}

From source file:Main.java

public static String pretty(String xml) {
    try {/*from   w  w  w.j  a  v  a 2 s  .c  om*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StreamResult result = new StreamResult(new StringWriter());
        Source source = new StreamSource(new StringReader(xml));
        transformer.transform(source, result);
        return result.getWriter().toString().replace("\r\n", "\n").replace("\n\r", "\n").replace("\r", "\n");
    } catch (TransformerFactoryConfigurationError | TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String prettyPrint(File file) {
    Transformer tf;// w  ww. java  2 s . co m
    try {
        tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult stringResult = new StreamResult(new StringWriter());
        tf.transform(new DOMSource(convertFileToDom(file)), stringResult);
        return stringResult.getWriter().toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.openmrs.module.metadatasharing.converter.ConverterEngine.java

private static String toXML(Node doc) throws SerializationException {
    try {// ww  w  . ja va 2 s  . com
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        transformer.transform(new DOMSource(doc), result);
        return result.getWriter().toString();
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}

From source file:com.syrup.storage.xml.XmlFactory.java

/**
 * Convert document to string. Helper method.
 * /*  w ww .j  a v  a 2s  .  co  m*/
 * @param document
 *            the document object.
 * @return String.
  * @throws java.io.IOException when unable to write the xml
  * @throws javax.xml.transform.TransformerException when unable to transform the document
 */
public static String documentToString(Document document) throws IOException, TransformerException {
    String soapRequest = null;

    if (document != null) {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, HTTP.UTF_8);
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(document);
        transformer.transform(source, result);
        return result.getWriter().toString();
    }

    return soapRequest;
}

From source file:Main.java

public static String getXmlString(Document xmlDoc)
        throws TransformerFactoryConfigurationError, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    //initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(xmlDoc);
    transformer.transform(source, result);

    return result.getWriter().toString();
}

From source file:Main.java

public static String getXmlString(Node xmlNode)
        throws TransformerFactoryConfigurationError, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    //initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(xmlNode);
    transformer.transform(source, result);

    return result.getWriter().toString();
}

From source file:Main.java

public static String getXml(Document doc) {
    try {/*from ww w  .  java  2  s. c  o  m*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        //initialize StreamResult with File object to save to file
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);

        String xmlString = result.getWriter().toString();
        return xmlString;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}