Example usage for javax.xml.transform.stream StreamResult getWriter

List of usage examples for javax.xml.transform.stream StreamResult getWriter

Introduction

In this page you can find the example usage for javax.xml.transform.stream StreamResult getWriter.

Prototype

public Writer getWriter() 

Source Link

Document

Get the character stream that was set with setWriter.

Usage

From source file:Main.java

/**
 * Return the contents of the node as a string. Any exceptions are ignored and the method returns null.
 * //w w w.  j  a  v a  2s  . co  m
 * @param node
 * @param indent
 *            Indent the XML when formatting
 * @return The node contents
 */
public static String getString(Node node, boolean indent) {
    Transformer transformer;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, (indent) ? "yes" : "no");

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

        return result.getWriter().toString();
    } catch (TransformerConfigurationException e) {
        //e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        //e.printStackTrace();
    } catch (TransformerException e) {
        //e.printStackTrace();
    }
    return "";
}

From source file:Main.java

/**
 * Formats an unformatted xml and returns a pretty format.
 * //from   www  .  jav a  2  s .  c om
 * @param unformattedXml
 *            the unformatted xml string
 * @return the xml in a pretty format
 * @throws TransformerException
 */
public static String formatXml(String unformattedXml) throws TransformerException {
    Document doc = parseXml(unformattedXml);

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    // initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    String xmlString = result.getWriter().toString();
    return xmlString;
}

From source file:Main.java

public static String getDomDocumentAsXml(org.w3c.dom.Document domDocument) {
    try {/*  w  w  w .  ja  v a  2 s .c o  m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult xmlOutput = new StreamResult(new StringWriter());
        transformer.transform(new DOMSource(domDocument), xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * To string.//w w w.  j  av  a 2s. c  o  m
 *
 * @param document
 *          the document
 * @param xmlDeclaration
 *          the xml declaration
 *
 * @return the string
 */
public static String toString(final Document document, final boolean xmlDeclaration) {
    if (document == null) {
        return null;
    }
    final StreamResult streamResult = new StreamResult(new StringWriter());
    transform(document, streamResult, xmlDeclaration);
    final String stringXml = streamResult.getWriter().toString();
    return stringXml;
}

From source file:Main.java

private static String getIndented(Document aDoc) {
    String outputXml = "";
    try {/* ww  w.  jav  a2  s  . c o  m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        //      tf.setAttribute("indent-number", new Integer(4));
        Transformer transformer;
        transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(aDoc);
        transformer.transform(source, result);
        outputXml = result.getWriter().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return outputXml;
}

From source file:Main.java

/**
 * Parses {@code org.w3c.dom.Document} and returns its xml serialization as {@code String}
 * @param dom/*ww w.jav a 2 s  . com*/
 * @return
 */
public static String DocumentToString(Document dom) {
    String xmlString = null;
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        //         initialize StreamResult with File object to save to file
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(dom);
        transformer.transform(source, result);

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

    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return xmlString;
}

From source file:gov.nih.nci.cacis.common.test.TestUtils.java

/**
 * Will get an XML Document as a String/*from   w ww  .  j  a  va  2 s.c  o  m*/
 *
 * @param doc XML Document
 * @return String xml as String
 * @throws javax.xml.transform.TransformerException exception
 */
public static String xmlToString(Document doc) throws TransformerException {
    final Transformer transformer = TransformerFactory.newInstance().newTransformer();

    // initialize StreamResult with File object to save to file
    final StreamResult result = new StreamResult(new StringWriter());
    final DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    return result.getWriter().toString();
}

From source file:Main.java

private static String getW3CXmlFromDoc(Document doc) {
    String xmlString = null;/* w  w  w.j  a va2 s . c  o  m*/
    Transformer transformer;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
        // transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // yes,
        // no
        transformer.setOutputProperty(OutputKeys.INDENT, "no");

        // initialize StreamResult with File object to save to file
        StreamResult xmlStream = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, xmlStream);
        xmlString = xmlStream.getWriter().toString();
    } catch (Exception ex) {
        logger.error(ex.getMessage());
    }

    return xmlString;
}

From source file:com.cuubez.visualizer.processor.ConfigurationProcessor.java

private static String getDocumentAsString(Document doc)
        throws TransformerFactoryConfigurationError, TransformerException {

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

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

    return result.getWriter().toString();
}

From source file:Main.java

public static String documentToString(Document doc) {
    StreamResult sr = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    try {/*  www  .j a va2s  .c  o  m*/
        _transformer.transform(source, sr);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sr.getWriter().toString();
}