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

/**
 * Return the contents of the node as a string. Any exceptions are ignored and the method returns null.
 * //from  www .  jav a2  s .c  o 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

public static String nodeToString(Node node) throws Exception {
    if (node == null) {
        return null;
    }/*from   w ww  .  j  av a2  s  .  co m*/
    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);
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    transformer.transform(source, result);
    return sw.toString();
}

From source file:Main.java

/**
 * Returns an XML representation of the provided node.
 *
 * @param node the node to be represented in XML.
 *
 * @return a string containing an XML representation of the
 * provided DOM node./*from   w  w  w . j  a  v  a  2  s.c  o m*/
 */
public static String nodeToXmlString(Node node) {

    try {

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

        DOMSource source = new DOMSource(node);
        StreamResult result = new StreamResult(new StringWriter());

        t.transform(source, result);

        return result.getWriter().toString();

    } catch (TransformerException e) {
        throw new IllegalStateException(e);
    } catch (TransformerFactoryConfigurationError e) {
        throw new IllegalStateException(e);
    }

}

From source file:Main.java

public static String prettyFormat(String input, int indent) {
    try {//from w w  w  .j a  va  2 s.c  om
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:Main.java

/**
 * Print the indented document.//from  w ww . j  av a  2  s .co m
 * 
 * @param stream
 * @param document
 * 
 * @throws UnsupportedEncodingException 
 * @throws TransformerExceptions 
 */
public static void printXMLFormat(Document document) throws UnsupportedEncodingException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 4); //$NON-NLS-1$
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$

    StreamResult streamResult = new StreamResult(); //$NON-NLS-1$

    DOMSource source = new DOMSource(document);
    transformer.transform(source, streamResult);
}

From source file:Main.java

public static Transformer getTransformer(boolean standalone, boolean indent, int indentNumber,
        boolean omitXmlDeclaration) throws TransformerException {
    TransformerFactory f = TransformerFactory.newInstance();
    f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
    if (indent) {
        f.setAttribute("indent-number", indentNumber);
    }/*  w w w. ja v  a2 s. c o m*/

    Transformer t = f.newTransformer();
    if (standalone) {
        t.setOutputProperty(OutputKeys.STANDALONE, "yes");
    }
    if (indent) {
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty("{xml.apache.org/xslt}indent-amount", "" + indentNumber);
    }
    if (omitXmlDeclaration) {
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }

    return t;
}

From source file:Main.java

public static void prettyFormatXml(final InputStream xml, final OutputStream os, final int indent) {
    try {//  w  w w  .  j  a  v  a  2 s . co  m
        final Source xmlInput = new StreamSource(xml);
        final StreamResult xmlOutput = new StreamResult(os);
        final TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        final Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String createString(Element element) {
    try {/*www  . j  a  v  a  2  s  .c om*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        DOMSource source = new DOMSource(element);
        Writer writer = new StringWriter();
        Result result = new StreamResult(writer);
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, result);
        return writer.toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String toString(final Document document) {
    try {/*from ww w. j av a2 s  .  co m*/
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        final TransformerFactory tf = TransformerFactory.newInstance();
        final Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
        //http://johnsonsolutions.blogspot.ca/2007/08/xml-transformer-indent-doesnt-work-with.html
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(document), new StreamResult(baos));

        final String result = baos.toString();
        return result;
    } catch (final TransformerException e) {
        throw new Error(e);
    }
}

From source file:Main.java

private static void transform(Document doc, StreamResult result) throws TransformerException {
    DOMSource source = new DOMSource(doc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
    transformer.transform(source, result);
}