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 elementToString(Element element) {
    Document document = element.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();

    // Client assumes/requires UTF-8 response.
    LSOutput lsOutput = domImplLS.createLSOutput();
    lsOutput.setEncoding("UTF-8");

    StringWriter stringWriter = new StringWriter();
    lsOutput.setCharacterStream(stringWriter);
    serializer.write(element, lsOutput);

    return stringWriter.toString();
}

From source file:Main.java

/**
 * Get DOM as a string//from w w w . j  a v a 2 s. com
 * @param doc
 * @return
 */
public static String getStringFromDoc(org.w3c.dom.Document doc) {
    if (doc == null) {
        System.out.println("XML document is null");
    }
    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.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                "http://commons.omniupdate.com/dtd/standard.dtd");
        transformer.transform(domSource, result);
        writer.flush();
        return writer.toString();
    } catch (TransformerException ex) {
        System.out.println("Transformer Exception");
        // ex.printStackTrace();
        return "Error in transformation";
    }
}

From source file:Main.java

/**
 * @param xml/*w  ww  .ja  v a  2s  .c om*/
 * @return pretty xml
 */
public static String prettyXml(String xml) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;
    try {
        Source source = new StreamSource(new StringReader(xml));
        StringWriter writer = new StringWriter();

        serializer = tfactory.newTransformer();
        // Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        serializer.transform(source, new StreamResult(writer));
        return writer.toString();
    } catch (Exception e) {
        return xml;
    }
}

From source file:Main.java

public static String obj2Xml(Object obj, String encoding) {
    String result = null;/*from  w w w. j  a  v  a  2  s. co  m*/
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        result = writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String documentToString(Document document) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;/*w  w w  .  ja va  2  s.  co m*/
    try {
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(document), new StreamResult(writer));
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return writer.getBuffer().toString().replaceAll("\n|\r", "");
}

From source file:Main.java

/**
 * Formats a xml string//w  ww  .  j  a  v a 2  s.c  om
 * @param input
 * @param indent
 * @return
 */
public static String prettyFormatXml(String input, int indent) {
    try {
        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");
        transformer.transform(xmlInput, xmlOutput);

        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return input;
}

From source file:Main.java

public static String transText(Node doc) throws Exception {
    TransformerFactory transfactory = TransformerFactory.newInstance();
    Transformer transformer = transfactory.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "yes");
    transformer.setOutputProperty("indent", "yes");

    Source source = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    transformer.transform(source, result);
    return sw.toString();
}

From source file:Hex.java

/**
 * Encodar binrt till hex/*from ww  w.  j  a  v a2 s .c  om*/
 *
 *@param dataStr bin-representation av data
 *@return Hex-representation av data
 **/
public static String encode(byte[] dataStr) {
    StringWriter w = new StringWriter();
    for (int i = 0; i < dataStr.length; i++) {
        int b = dataStr[i];
        w.write(hex[((b >> 4) & 0xF)]);
        w.write(hex[((b >> 0) & 0xF)]);
    }
    return w.toString();
}

From source file:Main.java

public static void dumpElement(Logger log, Element elem) {

    try {//w ww.  j  ava2s. c  om
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(elem), new StreamResult(buffer));

        log.debug(buffer.toString());
    } catch (TransformerFactoryConfigurationError | IllegalArgumentException | TransformerException ex) {
        log.error("Failed to dump element", ex);
    }
}

From source file:Main.java

public static String XML2String(Node node) {
    try {//  w  ww  . j  a  va2  s.c  o  m
        StringWriter writer = new StringWriter();
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        Source source = new DOMSource(node);
        Result result = new StreamResult(writer);
        trans.transform(source, result);
        return writer.toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}