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 toXmlString(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);
    writer.flush();/*from w  w w. j a v  a  2 s  .co m*/

    return writer.toString();
}

From source file:Main.java

public static String transformToString(Source source) {
    try {/*from  w ww  .ja va  2  s  . co m*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        StreamResult result = new StreamResult(new StringWriter());
        transformer.transform(source, result);
        return result.getWriter().toString();
    } catch (TransformerException ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String xmlToString(Document doc) throws Exception {

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc.getDocumentElement()), new StreamResult(writer));
    String result = writer.toString();
    System.out.println(result);/*from w w  w .  j a v  a 2s.co  m*/
    return result;
}

From source file:Main.java

public static String prettyFormat(String input, int indent, boolean isOmitXmlDeclaration) {
    try {//from  w  w  w  .ja va 2 s  . co  m
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        if (isOmitXmlDeclaration) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:Main.java

public static String domNodeToString(Node node) throws TransformerException {
    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

/**
 * /*ww w. ja  v  a 2s.com*/
 * @param doc
 * @return
 */
public static String document2String(Document doc) {
    try {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        // transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String object2Xml(Object obj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    StringWriter writer = new StringWriter();
    marshaller.marshal(new JAXBElement(new QName("xml"), obj.getClass(), obj), writer);

    return writer.toString();
}

From source file:Main.java

public static <T> String generateObject2XMLString(T object) throws JAXBException {

    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter sw = new StringWriter();
    jaxbMarshaller.marshal(object, sw);//from w ww.  j  a va2 s  . c  o m

    return sw.toString();
}

From source file:Main.java

public static int[] fileToIntArray(Context myContext, String filename, int size) {

    int[] array = new int[size];
    int i = 0;/*from  ww w . jav  a  2  s .  co  m*/
    FileInputStream inputStream;
    try {
        int c;
        inputStream = myContext.openFileInput(filename);
        StringWriter writer = new StringWriter();
        while ((c = inputStream.read()) != -1) {
            writer.append((char) c);
        }
        String ints[] = writer.toString().split("\n");
        for (String s : ints) {
            array[i++] = Integer.parseInt(s);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return array;
}

From source file:Main.java

public static String objectToXML(Class clazz, Object object) throws JAXBException {
    String xml = null;/*from   w  w  w. j a  v a2  s . co m*/
    JAXBContext context = JAXBContext.newInstance(clazz);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    Writer w = new StringWriter();
    m.marshal(object, w);
    xml = w.toString();
    return xml;
}