Example usage for javax.xml.transform Transformer setOutputProperty

List of usage examples for javax.xml.transform Transformer setOutputProperty

Introduction

In this page you can find the example usage for javax.xml.transform Transformer setOutputProperty.

Prototype

public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;

Source Link

Document

Set an output property that will be in effect for the transformation.

Usage

From source file:Main.java

/**
 * Transform xml Document to String/*from   w  ww.j  a  v  a2  s . c  o m*/
 * @param doc Xml Document
 * @return String representation
 */
public static String getContent(Document doc) {
    try {
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        transformer.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
    } catch (Exception ex) {
        throw new RuntimeException("Error converting to String", ex);
    }
}

From source file:Main.java

public static String prettyPrintXML(String source) {
    try {//w  w w.  ja v a 2s  . co  m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TransformerFactory tff = TransformerFactory.newInstance();
        Transformer tf;
        tf = tff.newTransformer();
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StreamResult result = new StreamResult(baos);
        tf.transform(new StreamSource(new StringReader(source)), result);
        return new String(baos.toByteArray());
    } catch (Exception e) {
        return source;
    }
}

From source file:Main.java

public static String sourceToString(Source source) throws IOException {

    try {//from  w  ww. ja  v  a 2 s  .  c  om
        Transformer trans = transFactory.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        Writer writer = new StringWriter();
        trans.transform(source, new StreamResult(writer));
        writer.flush();
        return writer.toString();
    } catch (TransformerException ex) {
        throw new IOException(ex);
    }
}

From source file:Main.java

public static void nodeToWriter(Node node, Writer writer) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    DOMSource source = new DOMSource(node);
    StreamResult result = new StreamResult(writer);
    transformer.transform(source, result);
}

From source file:Main.java

/**
 * Serialize an XML document into a String (for debug purpose only!). the
 * returned String could contains an error message instead if a problem as
 * occurred during the serialization. This method is intended for debug /
 * trace purpose because you really don't need to serialize XML at all in
 * your code unless your planing to output it to a file. In this case, use
 * {@link XmlHelper#writeXmlToFile(Document, File)} instead.
 *//*from   w  w w.ja  v a  2 s  . co  m*/
public static String dumpXml(Document document) {
    TransformerFactory factory = TransformerFactory.newInstance();
    try {
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException e) {
        return "XML dump failed: " + e.getMessage();
    }
}

From source file:Main.java

public static void printNode(Node node) {
    try {/*  ww w.  j  a  v a2s  .com*/
        // Set up the output transformer
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        // Print the DOM node

        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(node);
        trans.transform(source, result);
        String xmlString = sw.toString();

        System.out.println(xmlString);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Print the specified XML DOM node to the specified output stream
 * /*from   w w w  .ja v a  2  s.c o  m*/
 * @param n
 *            the node to print
 * @param os
 *            the output stream to print to
 * @throws Exception
 */
public static void printNode(Node n, OutputStream os) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    t.transform(new DOMSource(n), new StreamResult(os));
}

From source file:Main.java

/**
 * *****************************************
 * Convert XML document to string//from  w ww .j  a  v  a 2 s. co m
 * ******************************************.
 *
 * @param xmlDocument the xml document
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws TransformerException the transformer exception
 */
public static String converXmlDocToString(Document xmlDocument) throws IOException, TransformerException {
    String xmlString = "";

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(xmlDocument), new StreamResult(writer));
    xmlString = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return xmlString;
}

From source file:Main.java

public static String xmlDocToString(Document document) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(document), new StreamResult(writer));
    return writer.getBuffer().toString();
}

From source file:Main.java

public static String xmlToString(Node doc) {
    try {/*from   ww  w .  j  a  v  a2 s .  co  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);
    }
}