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 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");
    }/*from   w w  w. jav  a  2 s.  co  m*/
    trans.setOutputProperty(OutputKeys.STANDALONE, "no");

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

From source file:Main.java

public static String prettyPrintXML(String source) {
    try {/*  w w w  . j av  a 2s . co m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TransformerFactory tff = TransformerFactory.newInstance();
        Transformer tf;
        tf = tff.newTransformer();
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StreamResult result = new StreamResult(baos);
        tf.transform(new StreamSource(new StringReader(source)), result);
        return new String(baos.toByteArray());
    } catch (Exception e) {
        return source;
    }
}

From source file:Main.java

public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {/*from ww w .  jav  a2s  .  co m*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        System.err.println("nodeToString Transformer Exception");
    }
    return sw.toString();
}

From source file:Main.java

public static String formatXML(String xml) {
    try {/*from   w  w w  . j  a v a  2  s.c  o  m*/
        Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        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());
    } catch (Exception e) {

        return xml;
    }
}

From source file:Main.java

public static void init() {
    if (isInited) {
        return;//  w ww  .  j a  v  a2s.co  m
    }
    try {
        tx = TransformerFactory.newInstance().newTransformer();
        tx.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        tx.setOutputProperty(OutputKeys.INDENT, "no");
        isInited = true;
    } catch (TransformerFactoryConfigurationError | TransformerConfigurationException
            | IllegalArgumentException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Helper to turn on indentation for a {@link Transformer} that works correctly for
 * both Saxon and Xalan.//  w  w w .j  a v  a2  s  .  c  o m
 * 
 * @param transformer {@link Transformer} to configure
 * @param indent required indentation, where 0 or more provides indentation and negative
 *   numbers turns indentation off.
 */
public static void setIndentation(Transformer transformer, int indent) {
    if (indent >= 0) {
        String indentString = String.valueOf(indent);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        /* Set custom properties for both Saxon and Xalan at once.
         * This appears safe to do without having to check the underlying processor.
         */
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", indentString);
        transformer.setOutputProperty("{http://saxon.sf.net/}indent-spaces", indentString);
    } else {
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
    }
}

From source file:Main.java

public static String pretty(String xml) {
    try {/*from  w w  w.  j  a va 2  s  .c o  m*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StreamResult result = new StreamResult(new StringWriter());
        Source source = new StreamSource(new StringReader(xml));
        transformer.transform(source, result);
        return result.getWriter().toString().replace("\r\n", "\n").replace("\n\r", "\n").replace("\r", "\n");
    } catch (TransformerFactoryConfigurationError | TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void writeDocToFile(Document doc, String filename) throws Exception {
    Source source = new DOMSource(doc);
    File file = new File(filename);
    Result result = new StreamResult(file);
    TransformerFactory tFactory = TransformerFactory.newInstance();
    tFactory.setAttribute("indent-number", 2);
    Transformer xformer = tFactory.newTransformer();
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    //xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    xformer.transform(source, result);// w  w  w  .  ja v  a  2 s.c o m

    /*OutputFormat format = new OutputFormat(doc);
    format.setIndenting(true);
    format.setIndent(2);
    Writer output = new BufferedWriter( new FileWriter(filename) );
    XMLSerializer serializer = new XMLSerializer(output, format);
    serializer.serialize(doc);*/
}

From source file:Main.java

public static String transText(Node doc) throws Exception {
    TransformerFactory transfactory = TransformerFactory.newInstance();
    Transformer transformer = transfactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    Source source = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    transformer.transform(source, result);
    return sw.toString();
}

From source file:Main.java

public static void SaveDom(Document dom, String filepath) {
    try {/*from ww  w .ja v  a2  s  . c  om*/
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        DOMSource domSource = new DOMSource(dom);

        StreamResult streamResult = new StreamResult(new FileOutputStream(filepath));
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.transform(domSource, streamResult);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}