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 convertStreamToString(InputStream inputStream) throws IOException {
    if (inputStream != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {/*from www.  j  a va  2 s . co m*/
            Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 1024);
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            inputStream.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}

From source file:Main.java

public static String convertObjectToXML(Object object) {
    JAXBContext jaxbContext = null;
    Marshaller jaxbMarshaller = null;
    StringWriter stringWriter = new StringWriter();
    try {/* www. j ava2  s  . co m*/
        jaxbContext = JAXBContext.newInstance(object.getClass());
        jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        jaxbMarshaller.marshal(object, stringWriter);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    String xmlString = stringWriter.toString();

    return xmlString;

}

From source file:Main.java

public static String nodeToString(Node node) throws TransformerException {
    Source source = new DOMSource(node);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(source, result);
    return stringWriter.getBuffer().toString();
}

From source file:Main.java

public static String transform(String xml, File stylesheet) throws TransformerException {
    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
    transformer.transform(new StreamSource(xml), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static String getAsText(Node n) {
    try {//ww w .j  av a2 s  .  c o m
        Transformer t = TransformerFactory.newInstance().newTransformer();
        StringWriter out = new StringWriter();
        t.transform(new javax.xml.transform.dom.DOMSource(n), new javax.xml.transform.stream.StreamResult(out));
        return out.toString();
    } catch (Throwable t) {
        t.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String objectToXml(Object object) {
    if (null == object) {
        return NO_RESULT;
    }/*from w w  w .j  a  v  a 2s  .  com*/

    StringWriter sw = new StringWriter();
    JAXBContext jAXBContent = null;
    Marshaller marshaller = null;

    try {
        jAXBContent = JAXBContext.newInstance(object.getClass());
        marshaller = jAXBContent.createMarshaller();
        marshaller.marshal(object, sw);

        return sw.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return NO_RESULT;
}

From source file:Main.java

public static String transform(String xmlFilePath, String xsltFilePath) throws Exception {
    StreamResult result = new StreamResult(new StringWriter());
    StreamSource s = new StreamSource(new File(xsltFilePath));
    StreamSource xml = new StreamSource(new File(xmlFilePath));

    Transformer transformer = TransformerFactory.newInstance().newTransformer(s);
    transformer.transform(xml, result);//from   w ww .j a  v  a 2s .c om

    String response = result.getWriter().toString();

    return response;
}

From source file:Main.java

private static String parseObject2XmlString(Object object) {
    if (object == null) {
        return noResult;
    }/* w  w  w . j  a  va2  s  .c  om*/
    StringWriter sw = new StringWriter();
    JAXBContext jaxbContext;
    Marshaller marshaller;
    try {
        jaxbContext = JAXBContext.newInstance(object.getClass());
        marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(object, sw);
        return sw.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
        return noResult;
    }
}

From source file:Main.java

public static String nodeToString(Node node) throws TransformerException {
    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString().replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
}

From source file:Main.java

public static String toString(Document doc) throws TransformerException {
    StringWriter sw = new StringWriter();
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer transformer = tfactory.newTransformer();
    transformer.transform(new DOMSource(doc), new StreamResult(sw));

    return sw.toString();
}