Example usage for java.io StringWriter toString

List of usage examples for java.io StringWriter toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Return the buffer's current value as a string.

Usage

From source file:Main.java

@SuppressWarnings("rawtypes")
public static String ObjToXml(Object object, boolean isXmlFormat, Class... classesToBeBound)
        throws JAXBException, IOException {
    JAXBContext context = JAXBContext.newInstance(classesToBeBound);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isXmlFormat);
    m.setSchema(null);/*from   www. ja  v  a  2 s .  c  o m*/
    StringWriter sw = new StringWriter();
    m.marshal(object, sw);
    String result = sw.toString();
    sw.close();
    return result;
}

From source file:Main.java

public static String marshal(Object bean) throws JAXBException {
    StringWriter writer = new StringWriter();
    getMarshaller().marshal(bean, writer);
    return writer.toString();
}

From source file:Main.java

/**
 * This return exception as string./*from w w w . ja  v  a  2  s  .  c o m*/
 *
 * @param throwable Throwable
 * @return exception string
 */
public static String exceptionToString(Throwable throwable) {
    StringWriter errors = new StringWriter();
    throwable.printStackTrace(new PrintWriter(errors));
    return errors.toString();
}

From source file:Main.java

public static String getCurrentThreadStack() {
    StringWriter sw = new StringWriter();
    new Throwable("").printStackTrace(new PrintWriter(sw));
    return "\n" + sw.toString();
}

From source file:Main.java

License:asdf

public static String documentToString(Document document) {
    try {//from www .  j a va  2s. co  m
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        StringWriter sw = new StringWriter();
        trans.transform(new DOMSource(document), new StreamResult(sw));
        return sw.toString();
    } catch (TransformerException tEx) {
        tEx.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static <T> String asXml(T object) {
    try {/*from  w ww .  ja  v a2 s  .  co m*/
        JAXBContext jaxbContext = null;
        jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.marshal(object, writer);
        return writer.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String toXML(Object obj) {
    try {//from   w  ww  .  j  a  va 2  s  .c o  m
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        return writer.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static String getStackTrace(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    t.printStackTrace(pw);//from   ww  w.jav  a  2s  .c o m
    return sw.toString();
}

From source file:Main.java

public static String document2String(Document document, String encode) throws TransformerException {
    String xml = null;// www.  j  a  v a2  s . c om
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty("encoding", encode);
    transformer.setOutputProperty("indent", "yes");
    StringWriter sw = new StringWriter();
    transformer.transform(source, new StreamResult(sw));
    xml = sw.toString();
    return xml;
}

From source file:Main.java

public static String serialize(Node doc) throws Exception {
    StringWriter outText = new StringWriter();
    serialize(new DOMSource(doc), outText);
    return outText.toString();
}