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

/**
 * @see //http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java
 *///from w  w w.  j a v  a 2  s. c  om
public static String toXML(Document document, boolean format) throws TransformerException {
    if (format) {
        removeWhitespaceNodes(document.getDocumentElement());
    }
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 2);
    Transformer transformer = transformerFactory.newTransformer();

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    Writer out = new StringWriter();
    transformer.transform(new DOMSource(document), new StreamResult(out));
    return out.toString();
}

From source file:Main.java

/**
 * Save the given document to an output stream. Output is nicely formatted,
 * with child elements indented by 4 spaces.
 * /*from   w  w  w.jav  a2  s  . c o  m*/
 * @param doc
 *            Document to save
 * @param outputStream
 *            OutputStream to save to
 * @throws TransformerException
 * @throws IOException
 */
public static void saveDocumentToFormattedStream(Document doc, OutputStream outputStream)
        throws TransformerException, IOException {
    Source source = new DOMSource(doc);
    Result result = new StreamResult(outputStream);
    Transformer transformer = createTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
    transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$
    transformer.transform(source, result);
}

From source file:Main.java

/**
 * Saves a DOM to a human-readable XML file (4-space indentation, UTF-8).
 * <p>/*from w w  w .java2  s .  c  om*/
 * Contains workaround for various JVM bugs.
 * 
 * @param document
 *        The DOM
 * @param file
 *        The target XML file
 * @throws TransformerFactoryConfigurationError
 *         In case of an XML transformation factory configuration error
 * @throws TransformerException
 *         In case of an XML transformation error
 * @throws IOException
 *         In case of an I/O error
 */
public static void saveHumanReadable(Document document, File file)
        throws TransformerFactoryConfigurationError, TransformerException, IOException {
    // Various indentation and UTF8 encoding bugs are worked around here
    TransformerFactory factory = TransformerFactory.newInstance();
    factory.setAttribute("indent-number", new Integer(4));
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF8");
    transformer.transform(new DOMSource(document), new StreamResult(writer));
    writer.close();
}

From source file:Main.java

public static void marshalToStream(Document doc, OutputStream ostream, boolean indent) throws Exception {
    TransformerFactory transFac = TransformerFactory.newInstance();
    Transformer trans = transFac.newTransformer();
    if (indent) {
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    }/*w w  w . j  a  v a  2  s.c o  m*/
    trans.setOutputProperty(OutputKeys.STANDALONE, "no");

    trans.transform(new DOMSource(doc), new StreamResult(ostream));
}

From source file:Main.java

public static void marshalToStream(Element elm, OutputStream ostream, boolean indent) throws Exception {
    TransformerFactory transFac = TransformerFactory.newInstance();
    Transformer trans = transFac.newTransformer();
    if (indent) {
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    }//  w  ww .j  ava2  s .  c  om
    trans.setOutputProperty(OutputKeys.STANDALONE, "no");

    trans.transform(new DOMSource(elm), new StreamResult(ostream));
}

From source file:Main.java

public static String prettyPrintXML(String xml, int indentAmount)
        throws TransformerConfigurationException, TransformerException {

    Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();

    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indentAmount));
    Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
    StreamResult res = new StreamResult(new ByteArrayOutputStream());
    serializer.transform(xmlSource, res);

    return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray());
}

From source file:Main.java

public static String generateXml() {
    DocumentBuilder documentBuilder = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from   w w  w.  j a  v  a  2 s.  c  o  m*/
        documentBuilder = docFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();

        DOMSource docSource = new DOMSource(document);
        StreamResult result = new StreamResult();

        Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "no");

        result.setOutputStream(baos);
        transformer.transform(docSource, result);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return baos.toString();
}

From source file:Main.java

/**
 * Writes out XML.//w w w.java2  s. c  o  m
 * 
 * @param doc document
 * @param file target file
 * @throws TransformerException on transformer error
 */
public static void writeXml(Document doc, File file) throws TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(file);
    transformer.transform(source, result);
}

From source file:scala.c24.demo.java.C24DemoUtils.java

public static String getDocumentAsString(Resource resource) throws Exception {

    Document document = getDocument(resource);
    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));
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return output;
}

From source file:Main.java

public static String getContent(Node node, boolean omitXMLDeclaration) {
    try {/*ww  w  . ja  va2  s . c  o m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("indent", "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty("encoding", "UTF-8");
        if (omitXMLDeclaration) {
            transformer.setOutputProperty("omit-xml-declaration", "yes");
        }

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

        String cont = baos.toString("UTF8");

        baos.close();
        return cont;
    } catch (Exception ex) {
        return "";
    }
}