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 String toStringFromDoc(Element elem) {
    String result = null;//from  w ww . j  a v a  2 s.c om

    StringWriter strWtr = new StringWriter();
    StreamResult strResult = new StreamResult(strWtr);
    TransformerFactory tfac = TransformerFactory.newInstance();
    try {
        javax.xml.transform.Transformer t = tfac.newTransformer();
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html,
        // text
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        t.transform(new DOMSource(elem), strResult);
    } catch (Exception e) {
        System.err.println("XML.toString(Document): " + e);
    }
    result = strResult.getWriter().toString();
    try {
        strWtr.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static boolean write(Document doc, String path) {
    boolean result = false;

    try {/*from  w  w w .j  av a2s .c om*/
        DOMSource inputDoc = new DOMSource(doc);
        StreamResult sr = new StreamResult(path);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(inputDoc, sr);
        result = true;
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return result;
}

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 . j  ava2 s  .com*/
        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

/**
 * Parse file to xml type//from w ww. j  a v a 2  s.c  om
 *
 * @param transformer
 * @param source
 * @param file
 * @throws FileNotFoundException
 * @throws TransformerException
 */
public static void parseFileToXML(Transformer transformer, DOMSource source, File file)
        throws FileNotFoundException, TransformerException {
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    PrintWriter pw = new PrintWriter(file);
    StreamResult streamResult = new StreamResult(pw);
    transformer.transform(source, streamResult);
}

From source file:Main.java

public static String toXML(Document document, boolean format) throws Exception {

    if (format) {
        removeWhitespaceNodes(document.getDocumentElement());
    }//from w ww .j  a  v a 2  s.c o m
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 2);
    Transformer transformer = transformerFactory.newTransformer();

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    Writer out = new StringWriter();
    transformer.transform(new DOMSource(document), new StreamResult(out));
    return out.toString();
}

From source file:Main.java

public static String printDocument(Node doc) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    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", "2");
    StringWriter sw = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static String printDocument(Node doc) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    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", "2");
    StringWriter sw = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static String prettyPrintXML(String xml, int indentAmount)
        throws TransformerConfigurationException, TransformerException {

    Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();

    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indentAmount));
    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());
}

From source file:Main.java

public static String prettyFormat(String xml) {
    if (xml == null || xml.isEmpty() || !xml.contains("<")) {
        //          System.out.println("Why?"+xml.startsWith("<", 0));
        return xml;
    }/*from   w  ww  . ja v a  2  s  .c om*/
    try {
        Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        //serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "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()).replace("><", ">\n<");
    } catch (Exception e) {
        System.out.println("prettyFormat: Error.." + e.getMessage());
        //TODO log error
        return xml.replace("<", "\n<");
        //            return xml.replace("><", ">\n<");
    }
}

From source file:Main.java

public static String toString(Node doc) throws Exception {
    DOMSource domSource = new DOMSource(doc);
    StreamResult result = new StreamResult(new StringWriter());
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    //serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"users.dtd");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.transform(domSource, result);
    return result.getWriter().toString();
}