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:nl.igorski.lib.utils.data.XMLTool.java

/**
 * perform a full dump of a XML Document
 * for debugging purposes//from w w  w . ja  v a  2  s.com
 *
 * @param doc {Node}
 * @return {String}
 */
public static String nodeToString(Node doc) {
    Transformer transformer = null;

    try {
        transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    } catch (TransformerConfigurationException e) {
    }

    if (transformer != null) {
        //initialize StreamResult with File object to save to file
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);

        try {
            transformer.transform(source, result);
        } catch (TransformerException e) {
        }

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

From source file:com.petercho.Encoder.java

public static String formatXml(String xml) throws TransformerException {
    Source xmlInput = new StreamSource(new StringReader(xml));
    StringWriter stringWriter = new StringWriter();
    StreamResult xmlOutput = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 4);
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(xmlInput, xmlOutput);
    return xmlOutput.getWriter().toString();
}

From source file:ru.retbansk.utils.UsefulMethods.java

/**
 * For testing usage. Take unformatted xml string. Return pretty readable xml string
 * /*from w w  w.  ja v a 2s.  co  m*/
 * @param input take unformatted xml string
 * @param indent a number of white spaces before each line
 * @return pretty readable xml string
 */

public static String prettyFormat(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();
        // This statement works with JDK 6
        transformerFactory.setAttribute("indent-number", indent);

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Throwable e) {
        // You'll come here if you are using JDK 1.5
        // you are getting an the following exeption
        // java.lang.IllegalArgumentException: Not supported: indent-number
        // Use this code (Set the output property in transformer.
        try {
            Source xmlInput = new StreamSource(new StringReader(input));
            StringWriter stringWriter = new StringWriter();
            StreamResult xmlOutput = new StreamResult(stringWriter);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
            transformer.transform(xmlInput, xmlOutput);
            return xmlOutput.getWriter().toString();
        } catch (Throwable t) {
            return input;
        }
    }
}

From source file:Main.java

public static void pretty(Document document, OutputStream outputStream, int indent) throws IOException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/*from w w  w  .  ja v a 2  s  .  co  m*/
        transformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    if (indent > 0) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
    }
    Result result = new StreamResult(outputStream);
    Source source = new DOMSource(document);
    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

/**
 * Transforms a {@link Source} object into a {@link String} representation.
 * //ww  w . j  av  a  2 s  .c  o m
 * @param xml the source input.
 * @param pretty if {@code true} then pretty print with white space.
 * @param writer write the string to this {@link Writer}.
 * @throws TransformerException
 */
public static void toString(Source xml, boolean pretty, Writer writer) throws TransformerException {
    final TransformerFactory factory = TransformerFactory.newInstance();

    try {
        factory.setAttribute("{http://xml.apache.org/xalan}indent-amount", 2);
    } catch (IllegalArgumentException iae) {
        // try a different ident amount
        try {
            factory.setAttribute("indent-number", 2);
        } catch (IllegalArgumentException iae2) {
            // ignore
        }
    }

    final Transformer transformer = factory.newTransformer();
    if (pretty) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    }
    transformer.transform(xml, new StreamResult(writer));
}

From source file:Main.java

public static String printXMLNode(Node node) {
    try {//from   ww w. jav a2  s . co m
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", 2);
        Transformer transformer = tf.newTransformer();
        String omitDeclaration = node instanceof Document
                || node == node.getOwnerDocument().getDocumentElement() ? "no" : "yes";
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        output = removeEmptyLines(output); // .replaceAll("\n|\r", "");
        return output;
    } catch (TransformerException te) {
        throw new RuntimeException(te);
    }
}

From source file:Main.java

/**
 * Create and returns a new Transformer.
 *//* w w  w  .  java  2 s  .  co  m*/
public static Transformer createTransformer() {
    initFactories();

    final Transformer result;

    try {
        result = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        return null;
    }

    result.setOutputProperty(OutputKeys.METHOD, "xml");
    result.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    result.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    result.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    result.setOutputProperty(OutputKeys.INDENT, "yes");

    return result;
}

From source file:Main.java

public static void saveXml(Document modDoc, String path) throws TransformerException, IOException {
    Writer writer = null;//from   w ww. j a v  a  2  s .  c  o  m
    try {
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();
        Source src = new DOMSource(modDoc);
        writer = getFileWriter(path);
        Result dest = new StreamResult(writer);

        aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
        aTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
        aTransformer.transform(src, dest);
    } catch (TransformerException e) {
        throw e;
    } finally {

        writer.close();
    }

}

From source file:Main.java

public static void writeXmlFile(Document doc, File file, Entry<String, String>... outputKeys)
        throws TransformerFactoryConfigurationError, TransformerException {
    // Prepare the DOM document for writing
    Source source = new DOMSource(doc);

    // Prepare the output file
    Result result = new StreamResult(file);

    // Write the DOM document to the file
    Transformer xformer = TransformerFactory.newInstance().newTransformer();

    if (outputKeys != null) {

        for (Entry<String, String> entry : outputKeys) {

            xformer.setOutputProperty(entry.getKey(), entry.getValue());
        }/*w w w .j a  v a2  s.  c  o  m*/
    }

    xformer.transform(source, result);

}

From source file:com.omertron.traileraddictapi.tools.DOMHelper.java

/**
 * Write the Document out to a file using nice formatting
 *
 * @param doc The document to save/* w ww  .j a  va 2  s .c o  m*/
 * @param localFile The file to write to
 * @return
 */
public static boolean writeDocumentToFile(Document doc, String localFile) {
    try {
        TransformerFactory transfact = TransformerFactory.newInstance();
        Transformer trans = transfact.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, YES);
        trans.setOutputProperty(OutputKeys.INDENT, YES);
        trans.transform(new DOMSource(doc), new StreamResult(new File(localFile)));
        return true;
    } catch (TransformerConfigurationException ex) {
        LOG.warn(ERROR_WRITING_DOC, localFile, ex);
        return false;
    } catch (TransformerException ex) {
        LOG.warn(ERROR_WRITING_DOC, localFile, ex);
        return false;
    }
}