Example usage for java.io StringWriter getBuffer

List of usage examples for java.io StringWriter getBuffer

Introduction

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

Prototype

public StringBuffer getBuffer() 

Source Link

Document

Return the string buffer itself.

Usage

From source file:Main.java

/**
 * print document to string/*from   ww  w.  j a v  a2s  .  c  om*/
 * @param doc
 * @return
 * @throws TransformerException
 */
public static String toString(Document doc) throws TransformerException {
    Transformer tf = tff.newTransformer();
    StringWriter writer = new StringWriter();
    tf.transform(new DOMSource(doc), new StreamResult(writer));
    String output = writer.getBuffer().toString();
    return output;
}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {/*from   w  w  w.  j a v  a 2 s . c  o m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
        return output;
    } catch (TransformerException exc) {
        throw new RuntimeException("Unable to convert XML to String");
    }
}

From source file:Main.java

/**
 * Object to XML/*from w w w .j  av a  2 s.c o  m*/
 * @param object
 * @return
 */
public static String convertToXML(Object object) {
    try {
        if (!mMap.containsKey(object.getClass())) {
            JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            mMap.put(object.getClass(), marshaller);
        }
        StringWriter stringWriter = new StringWriter();
        mMap.get(object.getClass()).marshal(object, stringWriter);
        return stringWriter.getBuffer().toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String convertDocumentToString(Document doc)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    // Unquote below to remove XML declaration.
    // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    StringWriter writer = new StringWriter();

    transformer.transform(new DOMSource(doc), new StreamResult(writer));

    String output = writer.getBuffer().toString();

    return output;
}

From source file:Main.java

/**
 * Serialize an XML document into a String (for debug purpose only!). the
 * returned String could contains an error message instead if a problem as
 * occurred during the serialization. This method is intended for debug /
 * trace purpose because you really don't need to serialize XML at all in
 * your code unless your planing to output it to a file. In this case, use
 * {@link XmlHelper#writeXmlToFile(Document, File)} instead.
 *//*w w  w . j av a2 s  .  co  m*/
public static String dumpXml(Document document) {
    TransformerFactory factory = TransformerFactory.newInstance();
    try {
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException e) {
        return "XML dump failed: " + e.getMessage();
    }
}

From source file:Main.java

public static String convertDocumentToString(Document dom) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;//from   w  ww .  j a  v a 2s. c  om
    try {
        transformer = tf.newTransformer();
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(dom), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        return output;
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

static public String getPrettyPrint(Document doc) {
    try {// ww w .  ja v a2  s .com
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static String xmlDocToString(Document document) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(document), new StreamResult(writer));
    return writer.getBuffer().toString();
}

From source file:Main.java

/**
 * Converts an XML {@link org.w3c.dom.Document} to a string.
 *
 * @param doc the XML document/*from   ww  w.  j  a  va 2s  .com*/
 * @return the string representation of the XML document
 */
public static String documentToString(Document doc) {

    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

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

        return writer.getBuffer().toString();

    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace(System.err);
    }

    return "";
}

From source file:Main.java

/**
 * *****************************************
 * Convert XML document to string//ww  w. ja  v a 2 s. c  o  m
 * ******************************************.
 *
 * @param xmlDocument the xml document
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws TransformerException the transformer exception
 */
public static String converXmlDocToString(Document xmlDocument) throws IOException, TransformerException {
    String xmlString = "";

    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", "4");

    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(xmlDocument), new StreamResult(writer));
    xmlString = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return xmlString;
}