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 prettyPrintWithTrAX(Document document) throws TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    DOMSource domSource = new DOMSource(document);
    transformer.transform(domSource, streamResult);
    return stringWriter.toString();
}

From source file:Main.java

public static String serialize(Node doc) throws Exception {
    StringWriter outText = new StringWriter();
    serialize(new DOMSource(doc), outText);
    return outText.toString();
}

From source file:Main.java

/**
 * Process a w3c XML document into a Java String.
 * //from   ww  w  .  jav a 2  s .c o  m
 * @param outputDoc
 * @return
 */
public static String printToString(Document doc) {
    try {
        doc.normalizeDocument();
        DOMSource source = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(source, result);
        return writer.toString();
    } catch (Exception e) {
        // e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String toXml(Object jaxbElement, Map<String, Object> marshallerProps) {
    try {//  www .  ja  v  a2  s . com
        StringWriter stringWriter = new StringWriter();
        Marshaller marshaller = getJaxbMarshaller(jaxbElement.getClass(), marshallerProps);

        //         OutputFormat xmlOutputFormat = new OutputFormat();
        //         xmlOutputFormat.setCDataElements(new String[] {"^id"});
        //         xmlOutputFormat.setPreserveSpace(true);
        //         xmlOutputFormat.setIndenting(true);
        //         XMLSerializer xmlSerializer = new XMLSerializer(xmlOutputFormat);
        //         xmlSerializer.setOutputByteStream(System.out);
        //         marshaller.marshal(jaxbElement, xmlSerializer.asContentHandler());

        marshaller.marshal(jaxbElement, stringWriter);
        return stringWriter.toString();
    } catch (Exception e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
}

From source file:Main.java

/** Returns the entire value of a node (child nodes and text) as a String
 * @param node/* w  w  w  . j ava2s  .c  om*/
 * @return
 */
public static String nodeChildrenToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        NodeList nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++)
            t.transform(new DOMSource(nl.item(i)), new StreamResult(sw));
    } catch (TransformerException te) {
        System.out.println("nodeToString Transformer Exception");
    }
    return sw.toString().trim();
}

From source file:Main.java

static public String document2String(Node document) {
    if (document == null)
        return null;

    StringWriter stringWriter = new StringWriter();
    try {//from w  w w  .  java  2s . com
        identityTransformer.transform(new DOMSource(document), new StreamResult(stringWriter));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return stringWriter.toString();
}

From source file:Main.java

/** Returns the entier value of a node (child nodes and text) as a String
 * @param node//from   w w w .j ava2s . c om
 * @return
 */
public static String nodeChildrenToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        NodeList nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++)
            t.transform(new DOMSource(nl.item(i)), new StreamResult(sw));
        //      dimes.AgentGuiComm.GUICommunicator.sendLog(Level.WARNING, "", sw.toString());
    } catch (TransformerException te) {
        System.out.println("nodeToString Transformer Exception");
    }
    String test = sw.toString();
    return sw.toString().trim();
}

From source file:Main.java

public static String xml(TransformerFactory transformerFactory, Node node) {
    Writer writer = new StringWriter();
    DOMSource source = new DOMSource(node);
    StreamResult result = new StreamResult(writer);
    try {//w w  w .  java2  s .  com
        transformerFactory.newTransformer().transform(source, result);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return writer.toString().replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
}

From source file:Main.java

public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException {
    if (node == null)
        return "";
    StringWriter sw = new StringWriter();
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static String doc2str(Document doc) throws TransformerConfigurationException, TransformerException {
    StreamResult result = new StreamResult(new StringWriter());
    doc2stream(doc, result);//from w  w  w. j a  v a 2 s.co m
    String xmlString = result.getWriter().toString();
    return xmlString;
}