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

/**
 * Print XML in "pretty" format./* w w  w.j a va2 s  .c om*/
 * 
 * @param in an XML input stream
 * @param out where the result will go
 * @param indent the number of characters to indent
 */
public static void prettyPrintXml(InputStream in, OutputStream out, int indent) {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    TransformerFactory tfactory = TransformerFactory.newInstance();

    // set indent spaces on factory
    //        tfactory.setAttribute("indent-number", new Integer(indent));

    try {
        DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(in);
        Transformer serializer = tfactory.newTransformer();

        // turn on indenting on transformer (serializer)
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
                //                  String.valueOf(indent));
                "2");

        serializer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "utf-8")));
    } catch (Exception e) {

        // this is fatal, just dump stack and throw runtime exception
        e.printStackTrace();

        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String xmltoString(final Document document) throws TransformerException {
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = 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", "2");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.transform(new DOMSource(document.getDocumentElement()), streamResult);
    return stringWriter.toString();
}

From source file:Main.java

/** 
 * Returns the XML string representation of a Node.
 * @param node Node to convert into XML string. 
 * @param omitXmlDeclaration <tt>true</tt> to remove the XML declaration from the string. 
 * @return The XML string representation of the input node. 
 * @throws TransformerConfigurationException 
 * @throws TransformerException //  www  .ja va  2s.  c o m
 */
public static String getXMLString(Node node, boolean omitXmlDeclaration)
        throws TransformerConfigurationException, TransformerException {
    StringWriter writer = new StringWriter();
    DOMSource domSource = new DOMSource(node);
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
    serializer.transform(domSource, result);
    return (writer.toString());
}

From source file:Main.java

/**
 * @param xml// w  w w .j av  a  2  s. c  om
 */
public static void prettyPrint(String xml) {
    Source xmlInput = new StreamSource(new StringReader(xml));
    StreamResult xmlOutput = new StreamResult(new StringWriter());
    Transformer transformer = null;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    try {
        transformer.transform(xmlInput, xmlOutput);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String formattedxml = xmlOutput.getWriter().toString();
    System.out.println(formattedxml);

}

From source file:Main.java

/**
 * @return a transformer that indents entries by 4 characters (never null)
 *///from  w w  w .j a v  a2  s .  c  om
public static final Transformer createIndentingTransformer() {
    Transformer xformer;
    try {
        transformerFactory.setAttribute("indent-number", 4);
        xformer = transformerFactory.newTransformer();
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    return xformer;
}

From source file:Main.java

/**
 * @param xml//from ww  w . ja v  a 2 s  . co  m
 */
public static String prettyPrintToString(String xml) {
    Source xmlInput = new StreamSource(new StringReader(xml));
    StreamResult xmlOutput = new StreamResult(new StringWriter());
    Transformer transformer = null;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    try {
        transformer.transform(xmlInput, xmlOutput);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String formattedxml = xmlOutput.getWriter().toString();
    return formattedxml;

}

From source file:Main.java

/**
 * Saves the XML file to disk.//from w w w. j  a va2  s  . c  o m
 * @param fileName The name of the file.
 * @param doc The XML Document to save to disk.
 * @throws TransformerConfigurationException
 * @throws TransformerException 
 */
private static void store(String fileName, Document doc)
        throws TransformerConfigurationException, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    transformer.transform(new DOMSource(doc), new StreamResult(new File(fileName)));
}

From source file:Main.java

/**
 * Dump a {@link Document} or {@link Node}-compatible object to the given {@link OutputStream} (e.g. System.out).
 *
 * @param _docOrNode {@link Document} or {@link Node} object
 * @param _outStream {@link OutputStream} to print on
 * @throws IOException on error/*from   w  ww .  ja v  a  2s.  c  o  m*/
 */
public static void printDocument(Node _docOrNode, OutputStream _outStream) throws IOException {
    if (_docOrNode == null || _outStream == null) {
        throw new IOException("Cannot print (on) 'null' object");
    }

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        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");

        transformer.transform(new DOMSource(_docOrNode),
                new StreamResult(new OutputStreamWriter(_outStream, "UTF-8")));
    } catch (UnsupportedEncodingException | TransformerException _ex) {
        throw new IOException("Could not print Document or Node.", _ex);
    }

}

From source file:Main.java

public static String getXMLAsString(Element eElement) throws Exception {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(eElement);
    transformer.transform(source, result);

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

From source file:Main.java

public static void setCommonOutputProperties(final Transformer transformer, final boolean indentOutput)
        throws TransformerConfigurationException {
    transformer.setOutputProperty(OutputKeys.METHOD, XML);
    transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8);
    transformer.setOutputProperty(OutputKeys.VERSION, VERSION);
    if (indentOutput) {
        transformer.setOutputProperty(OutputKeys.INDENT, YES);
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
    } else {//  ww  w  .j  a va2 s .  c  o m
        transformer.setOutputProperty(OutputKeys.INDENT, NO);
    }
}