Example usage for javax.xml.transform OutputKeys INDENT

List of usage examples for javax.xml.transform OutputKeys INDENT

Introduction

In this page you can find the example usage for javax.xml.transform OutputKeys INDENT.

Prototype

String INDENT

To view the source code for javax.xml.transform OutputKeys INDENT.

Click Source Link

Document

indent = "yes" | "no".

Usage

From source file:Main.java

public static boolean write(String filename, Document document, boolean addDocType) {
    try {//from  w  w  w.j a  v  a 2 s  . co m
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", new Integer(4));
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        if (addDocType) {
            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
                    "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN");
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                    "http://java.sun.com/dtd/facelet-taglib_1_0.dtd");
        }
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source,
                new StreamResult(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)))));
        return true;
    } catch (Exception e) {
        return false;
    }

}

From source file:Main.java

/**
 * *****************************************
 * Convert XML document to string//w ww . jav a2 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

/**
 * Transform a org.w3c.dom.Node to a String
 * @param node - the Node/*from  w  w  w  .  jav  a  2s . c o  m*/
 * @param omitXmlDeclaration - omit XML declaration
 * @param prettyPrint - apply indentation
 * @return the String result
 * @throws TransformerException
 */
public static String elementToString(Node node, boolean omitXmlDeclaration, boolean prettyPrint) {
    String result = null;
    try {
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        StringWriter buffer = new StringWriter();
        if (omitXmlDeclaration) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
        if (prettyPrint) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        }
        transformer.transform(new DOMSource(node), new StreamResult(buffer));
        result = buffer.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:Main.java

public static void saveDocument(Document document, String path)
        throws TransformerConfigurationException, TransformerFactoryConfigurationError,
        TransformerFactoryConfigurationError, TransformerException, IOException {

    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    DOMSource dom = new DOMSource(document);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 4);

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(dom, sr);/*from  w  w w . j a v  a 2  s.  c  o m*/

    String string = sw.toString();
    FileWriter fw = new FileWriter(new File(path));
    fw.write(string);
    fw.close();
}

From source file:Main.java

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

From source file:Main.java

/**
 * Converts an XML node to a string.//  w  ww. j a v a  2  s.  c o  m
 * @param node the XML node
 * @param prettyPrint true to pretty print, false not to
 * @return the string
 */
public static String toString(Node node, boolean prettyPrint) {
    Map<String, String> properties = new HashMap<String, String>();
    if (prettyPrint) {
        properties.put(OutputKeys.INDENT, "yes");
        properties.put("{http://xml.apache.org/xslt}indent-amount", "2");
    }
    return toString(node, properties);
}

From source file:Main.java

public static Transformer buildTransformer() throws TransformerConfigurationException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    return transformer;
}

From source file:Main.java

/**
 * Formats an unformatted xml and returns a pretty format.
 * /*  w ww .ja v 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

/**
 * @param xml/*w  w w . j av a 2  s .  c o m*/
 * @return pretty xml
 */
public static String prettyXml(String xml) {
    Transformer serializer;
    try {
        Source source = new StreamSource(new StringReader(xml));
        StringWriter writer = new StringWriter();

        serializer = transFactory.newTransformer();
        // Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        serializer.transform(source, new StreamResult(writer));
        return writer.toString();
    } catch (Exception e) {
        return xml;
    }
}

From source file:Main.java

/**
 * Pretty print an XML. Use with caution as this drains the source if it is
 * a StreamSource./*w w  w.j a v a  2 s  . c  om*/
 * @param source the XML source
 * @return a String with readable XML
 */
public static String prettyPrint(final Source source) {
    try {
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StringWriter writer = new StringWriter();
        serializer.transform(source, new StreamResult(writer));
        return writer.toString();
    } catch (TransformerException e) {
        return e.getMessage();
    }
}