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 getDtdAsString(String uri) throws IOException {
    Reader in = null;//from   www .j  a  v a  2  s .c  om
    if ((uri.startsWith("http")) || (uri.startsWith("ftp")) || (uri.startsWith("file:")))
        in = new InputStreamReader(new URL(uri).openStream());
    else {
        in = new FileReader(uri);
    }
    StringWriter out = new StringWriter();
    char[] buffer = new char[4096];
    for (int count = in.read(buffer); count != -1; count = in.read(buffer)) {
        out.write(buffer, 0, count);
    }
    return out.getBuffer().toString();
}

From source file:Main.java

/**
 * Transform xml Document to String/* w ww  .  jav  a 2  s. c o m*/
 * @param doc Xml Document
 * @return String representation
 */
public static String getContent(final Document doc) {
    try {
        final StringWriter sw = new StringWriter();
        final TransformerFactory tf = TransformerFactory.newInstance();
        final 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.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
    } catch (Exception ex) {
        throw new RuntimeException("Error converting to String", ex);
    }
}

From source file:Main.java

public static String toXMLString(Node node) {
    if (node == null) {
        return "";
    }//from  w  w  w  .  j  av a 2s.co m
    try {
        Transformer tran = tf.newTransformer();
        tran.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter swriter = new StringWriter();
        Source src = new DOMSource(node);
        Result res = new StreamResult(swriter);
        tran.transform(src, res);
        return swriter.getBuffer().toString();
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static String xmlParaString(Node doc) {
    DOMSource xmlSource = new DOMSource(doc);

    StringWriter sw = new StringWriter();
    Result result = new StreamResult(sw);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer;/*from www. ja v a 2  s  .  c  o  m*/

    try {
        transformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }

    try {
        transformer.transform(xmlSource, result);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return sw.toString();
}

From source file:Main.java

public static String toXxml(Object bean) {
    StringWriter stringWriter = null;
    try {/*from www . ja  v a 2 s .  c o  m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(bean.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        stringWriter = new StringWriter();
        marshaller.marshal(bean, stringWriter);
        String result = stringWriter.toString();
        // remove xml declaration
        result = result.replaceFirst(".*\n", "");
        return result;
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    } finally {
        if (stringWriter != null)
            try {
                stringWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

From source file:Main.java

public static <T> String print(T xml, Class<?>... clazz) throws JAXBException {
    //Create JAXB Context
    JAXBContext jc = JAXBContext.newInstance(clazz);

    //Create marshaller
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter writer = new StringWriter();
    marshaller.marshal(xml, writer);/*from  ww w  . ja  va 2 s. c o  m*/

    return writer.toString();
}

From source file:Main.java

public static String convertDocumentToString(Document doc) throws IOException, TransformerException {
    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

protected static Result internalTransform(Document doc, Templates templates, Result r, boolean trace) {
    StringWriter sw = new StringWriter();

    try {//from  w  w  w . j a  v a 2s  . c  om
        Transformer transformer = templates.newTransformer();

        transformer.transform(new DOMSource(doc), r);

        sw.close();
        return r;
    } catch (Throwable th) {
        th.printStackTrace();
        return r;
    }
}

From source file:Main.java

public static String documentToString(Document doc, String xslName) {
    StringWriter sw = new StringWriter();
    try {/*from   w  w  w  .  j a  v  a 2  s  .  c o m*/
        TransformerFactory transfactory = TransformerFactory.newInstance();
        Transformer transformer = transfactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        Source source = new DOMSource(doc.getDocumentElement());
        Result result = new StreamResult(sw);
        transformer.transform(source, result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return head + xslName + sw.toString();
}

From source file:Main.java

public static String formatDocumentAsString(Document document) throws TransformerException {
    Writer out = new StringWriter();
    formatDocument(document, out);/*from   w ww . ja v  a  2 s  .  c om*/
    String formattedXML = out.toString();
    return formattedXML;
}