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

/**
 * @param xml/*from   w w  w  .  j a v a2 s. c  o  m*/
 */
public static void prettyPrint(String xml) {
    Source xmlInput = new StreamSource(new StringReader(xml));
    StreamResult xmlOutput = new StreamResult(new StringWriter());
    Transformer transformer = null;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    try {
        transformer.transform(xmlInput, xmlOutput);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String formattedxml = xmlOutput.getWriter().toString();
    System.out.println(formattedxml);

}

From source file:Main.java

/**
 * Convert XML DOM document to a XML string representation
 *
 * @param doc/*from w w w. j  ava  2 s  . com*/
 *            XML DOM document
 * @return XML string
 * @throws Exception
 *             in error case
 */
public static String xmlDOMDocumentToString(Document doc) throws Exception {
    if (doc == null) {
        throw new RuntimeException("No XML DOM document (null)!");
    }
    StringWriter stringWriter = new StringWriter();
    String strDoc = null;

    try {
        StreamResult streamResult = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        // transformerFactory.setAttribute("nIndent-number", new Integer(4));
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.transform(new DOMSource(doc.getDocumentElement()), streamResult);
        stringWriter.flush();
        strDoc = stringWriter.toString();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Parsing of XML DOM document failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (stringWriter != null) {
            stringWriter.close();
            stringWriter = null;
        }
    }

    return strDoc;
}

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

public static String prettyPrint(DOMSource domSource) {
    try {//from  www  .j av  a 2s.  c o m
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StreamResult streamResult = new StreamResult(new StringWriter());
        transformer.transform(domSource, streamResult);

        return streamResult.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException("Error while pretty printing xml", e);
    }
}

From source file:Main.java

public static void saveDocument(Document document, String path)
        throws TransformerConfigurationException, TransformerFactoryConfigurationError,
        TransformerFactoryConfigurationError, TransformerException, IOException {

    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    DOMSource dom = new DOMSource(document);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 4);

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(dom, sr);//from w w w.  j  ava2  s  . c o  m

    String string = sw.toString();
    FileWriter fw = new FileWriter(new File(path));
    fw.write(string);
    fw.close();
}

From source file:Main.java

public static String bean2Xml(Object bean) {
    String xmlString = null;/*from w  w w  . java  2  s  .  c  o m*/
    JAXBContext context;
    StringWriter writer;
    if (null == bean)
        return xmlString;
    try {
        context = JAXBContext.newInstance(bean.getClass());
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);//
        //m.setProperty(Marshaller.JAXB_ENCODING, "gb2312");//
        //m.setProperty(Marshaller.JAXB_ENCODING, "GBK");//
        m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");//
        m.setProperty(Marshaller.JAXB_FRAGMENT, false);//
        writer = new StringWriter();
        m.marshal(bean, writer);
        xmlString = writer.toString();
        return xmlString;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return xmlString;
}

From source file:helper.SerializationHelper.java

public static String serializeGridTopology(ActorTopology topology) {
    ObjectMapper mapper = new ObjectMapper();
    Writer strWriter = new StringWriter();
    try {//from  w w  w.ja  va 2s. c  o m
        mapper.writeValue(strWriter, topology);
    } catch (Exception e) {
        System.out.println(e);
    }
    return strWriter.toString();
}

From source file:Main.java

/**
 * @param xml//from www.j a va  2s .  co  m
 */
public static String prettyPrintToString(String xml) {
    Source xmlInput = new StreamSource(new StringReader(xml));
    StreamResult xmlOutput = new StreamResult(new StringWriter());
    Transformer transformer = null;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    try {
        transformer.transform(xmlInput, xmlOutput);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String formattedxml = xmlOutput.getWriter().toString();
    return formattedxml;

}

From source file:Main.java

static String readFully(Reader reader) throws IOException {
    try {//from   w ww. ja v a 2s. c  o m
        StringWriter writer = new StringWriter();
        char[] buffer = new char[1024];
        int count;
        while ((count = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, count);
        }
        return writer.toString();
    } finally {
        reader.close();
    }
}

From source file:Main.java

/**
 * Object to XML//from  w w w.j a v a2s .  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;
}