Example usage for java.io StringWriter StringWriter

List of usage examples for java.io StringWriter StringWriter

Introduction

In this page you can find the example usage for java.io StringWriter StringWriter.

Prototype

public StringWriter() 

Source Link

Document

Create a new string writer using the default initial string-buffer size.

Usage

From source file:Main.java

public static String XMLtoString(Document doc) throws Exception {
    try {//from   ww w  . ja  va 2  s .c  o m
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tf.newTransformer();
        // below code to remove XML declaration
        // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException te) {
        throw new Exception(te.getMessageAndLocation());
    }
}

From source file:Main.java

public static String getDocumentAsString(Document doc) throws TransformerException {
    StringWriter sw = new StringWriter();
    tf.newTransformer().transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static String getStringFromDocument(Document doc) throws Exception {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    return writer.toString();
}

From source file:Main.java

public static String prettyPrint(Node node) {
    try {/*from   w  w w  .ja  va 2  s  .  c om*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        return writer.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Converts a dom to a String/*from w w w .  j  a  va 2 s.c  o m*/
 * 
 * @param dom
 *            dom to convert
 * @param outputProperties
 *            the properties for the String representation of the XML
 * @return the dom as a String
 */
public static String writeDomToString(Document dom, Properties outputProperties) {
    try {
        StringWriter ret = new StringWriter();
        TransformerFactory transFact = TransformerFactory.newInstance();
        //                transFact.setAttribute("indent-number", 2);
        Transformer transformer = transFact.newTransformer();
        if (outputProperties != null)
            transformer.setOutputProperties(outputProperties);
        DOMSource source = new DOMSource(dom);
        StreamResult result = new StreamResult(ret);
        transformer.transform(source, result);
        return ret.toString();
    } catch (Exception e) {
        throw new RuntimeException("Could not write dom to string!", e);
    }
}

From source file:Main.java

public static String transformXmlToString(Document importPackageDocument)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException,
        IOException {/*from  w  w w.  java 2s  .co m*/
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    StringWriter writer = new StringWriter();
    javax.xml.transform.Result result = new StreamResult(writer);
    Source source = new DOMSource(importPackageDocument);
    transformer.transform(source, result);
    writer.close();
    String xml = writer.toString();
    return xml;
}

From source file:Main.java

/**
 * Convert the document to an array of bytes.
 * /*from   w  ww.  ja  v  a  2s .c  o  m*/
 * @param doc
 *            The XML document.
 * @param encoding
 *            The encoding of the output data.
 * 
 * @return The XML document as an array of bytes.
 * 
 * @throws TransformerException
 *             If there is an error transforming to text.
 */
public static byte[] asByteArray(Document doc, String encoding) throws TransformerException {
    final Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    final StringWriter writer = new StringWriter();
    final Result result = new StreamResult(writer);
    final DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    return writer.getBuffer().toString().getBytes();
}

From source file:AIR.Common.Json.JsonHelper.java

public static <T> String serialize(T obj) throws JsonGenerationException, JsonMappingException, IOException {
    if (obj == null)
        return null;

    ObjectMapper mapper = new ObjectMapper();

    String json = null;/*w  w  w .j a v a 2s .c  om*/

    StringWriter sw = new StringWriter();
    mapper.writeValue(sw, obj);

    json = sw.toString();
    sw.close();

    return json;
}

From source file:net.fizzl.redditengine.data.FlairListing.java

public static FlairListing fromInputStream(InputStream is) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    return fromString(writer.toString());
}

From source file:net.fizzl.redditengine.data.ModlogListing.java

public static ModlogListing fromInputStream(InputStream is) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    return fromString(writer.toString());
}