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 final String obj2xml(final Object obj, final boolean formatted) throws JAXBException {
    StringWriter writer = new StringWriter();
    JAXBContext contextOut = JAXBContext.newInstance(obj.getClass());
    Marshaller marshallerOut = contextOut.createMarshaller();
    marshallerOut.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatted);
    marshallerOut.marshal(obj, writer);/*www.ja  v a2s.  com*/

    return new String(writer.getBuffer());
}

From source file:Main.java

public static String documentToString(Document doc) throws IOException {
    StringWriter writer = new StringWriter();
    writeDocument(doc, writer);/*from  www.ja v a2s  .c o  m*/
    return writer.toString();
}

From source file:Main.java

public static String format(Document document) {
    try {//from w w w .j  a v  a2s . c  o  m

        OutputFormat format = new OutputFormat(document);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setIndent(2);
        Writer out = new StringWriter();
        XMLSerializer serializer = new XMLSerializer(out, format);
        serializer.serialize(document);

        return out.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Pretty format a given XML document//  w  ww . j  a v a2  s .  co  m
 *
 * @param strInput
 *            Valid XML document (No validity check yet!)
 * @param nIndent
 *            Indent
 * @return Formatted XML document
 * @throws Exception
 *             in error case
 */
public static String prettyFormat(String strInput, int nIndent) throws Exception {
    try {
        Source xmlInput = new StreamSource(new StringReader(strInput));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", nIndent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(nIndent));
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Pretty formatting: " + e.getMessage(), LogLevel.Error);
        throw e;
    }
}

From source file:Main.java

/**
 * Transforms an XML to a String/*from  w  w  w  .j a  v a 2  s. co m*/
 * @param node XML node
 * @return String represenation of XML
 */
public static String printXML(Node node) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;
    try {
        serializer = tfactory.newTransformer();

        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        StringWriter output = new StringWriter();
        serializer.transform(new DOMSource(node), new StreamResult(output));
        return output.toString();
    } catch (TransformerException e) {

        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Convert XML {@link Document} to its string representation.
 *
 * @param document for conversion/* w ww  . ja  va 2  s  .c om*/
 * @return - string representation of XML {@link Document}
 * @throws Exception - if {@link DocumentBuilder} is not initialized
 */
public static String xmlToString(Document document) throws Exception {
    if (transformer == null) {
        throw new Exception("Transformer is null");
    }
    Source xmlSource = new DOMSource(document);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    transformer.transform(xmlSource, result);
    return stringWriter.toString();
}

From source file:Main.java

public static String getPrettyXml(byte[] xml) {
    try {/*  www  .jav a2s.c om*/
        String unformattedXml = new String(xml);
        final Document document = parseXmlFile(unformattedXml);

        OutputFormat format = new OutputFormat(document);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setIndent(2);
        Writer out = new StringWriter();
        XMLSerializer serializer = new XMLSerializer(out, format);
        serializer.serialize(document);

        return out.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Returns the String-Representation of the given DOM-Node as well-formed DOM-Document.
 *
 * @param node DOM-Node to print//  www. j av  a  2  s.  c  o  m
 * @param indent if true resulting XML is endented
 * @return <code>String</code> - Node as XML-String
 * @throws Exception on error
 */
public static String domNode2String(Node node, boolean indent) throws Exception {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");

    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(node);
    transformer.transform(source, result);

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

    return xmlString;
}

From source file:Main.java

public static String xmlToString(Node doc) {
    try {/*from   w  w  w  .  j  a v  a 2 s . c o  m*/
        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.INDENT, "yes");
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String serializeDocument(Document document) throws Exception {
    // Serialize XML document to String.
    StringWriter writer = new StringWriter();
    StreamResult streamResult = new StreamResult(writer);

    DOMSource domSource = new DOMSource(document);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.transform(domSource, streamResult);

    return writer.toString();
}