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 File saveToFile(File file, Document document) throws TransformerException {
    // Prepare the DOM document for writing
    Source source = new DOMSource(document);

    // Prepare the output file
    Result result = new StreamResult(file.toURI().getPath());

    // Write the DOM document to the file
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.transform(source, result);//w  w  w .j  av a 2  s .c om

    return file;
}

From source file:Main.java

public static void writeTransformedXml(Document doc, OutputStream output, InputStream style)
        throws TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    StreamSource stylesource = new StreamSource(style);
    Transformer transformer = transformerFactory.newTransformer(stylesource);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(output);
    transformer.transform(source, result);
}

From source file:Main.java

public static String prettyPrint(Document doc) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;//from   w ww . ja v a2 s.c  o  m
    StringWriter writer = new StringWriter();
    try {
        serializer = tfactory.newTransformer();
        //Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //$NON-NLS-1$ //$NON-NLS-2$

        serializer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Converts an XML document to a formatted XML string.
 * /*from  w ww  .j av a2 s .  c o  m*/
 * @param doc The document to format.
 * @return Formatted XML document.
 */
public static String toString(Document doc) {
    if (doc == null) {
        return "";
    }

    try {
        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.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception e) {
        return e.toString();
    }
}

From source file:Main.java

/**
 * Write an XML document out to an output stream in UTF-8
 * @param document - the document to write.
 * @param outputStream - the stream to which the XML will be written
 * @param indent - the indent level./*from   w w  w.  j  a v a  2s  .  c  o m*/
 */
public static void writeXmlDocumentToStream(Document document, OutputStream outputStream, int indent) {

    try {
        Transformer transformer = transformerFactory.get().newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, UTF8);
        if (indent > 0) {
            transformer.setOutputProperty(OutputKeys.INDENT, YES);
            transformer.setOutputProperty(INDENT_AMOUNT, Integer.toString(indent));
        }
        Result result = new StreamResult(outputStream);
        Source source = new DOMSource(document);
        transformer.transform(source, result);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static Transformer createXmlTransformer() throws Exception {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    return transformer;
}

From source file:Main.java

public static String getFormattedXml(String xmlString) {

    // ///////////////////////////////////////////////////////////////
    //   Declarations
    // ///////////////////////////////////////////////////////////////

    Source xmlInput = null;//w  ww. j a va 2s. c o m
    StringWriter stringWriter = null;
    StreamResult xmlOutput = null;

    TransformerFactory transformerFactory = null;
    Transformer transformer = null;

    String formattedXml = null;

    // ///////////////////////////////////////////////////////////////
    //   Code
    // ///////////////////////////////////////////////////////////////

    try {

        xmlInput = new StreamSource(new StringReader(xmlString));
        stringWriter = new StringWriter();
        xmlOutput = new StreamResult(stringWriter);

        transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 4);

        transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);

        formattedXml = xmlOutput.getWriter().toString();
    } catch (Exception e) {

        // To Do: Handle Exception..
    }

    return formattedXml;
}

From source file:Main.java

public static void serialize(Node n, StreamResult sr) throws IOException, TransformerException {
    TransformerHandler hd = newTransformerHandler();
    Transformer serializer = hd.getTransformer();

    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(n);
    serializer.transform(source, sr);//from ww w  .j av  a2  s  .c  o  m
}

From source file:Main.java

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

From source file:Main.java

/**
 * Makes the specified XML string pretty.
 * //from w  w w  .  jav a2  s. com
 * @param xmlString
 *            The XML string to process.
 * @return Pretty-printed XML string.
 */
public static final String prettyPrint(final String xmlString) {
    try (InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes());
            ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        final Document document = documentBuilder.parse(inputStream);

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

        final DOMSource xmlSource = new DOMSource(document);
        final StreamResult outputTarget = new StreamResult(baos);
        serializer.transform(xmlSource, outputTarget);
        return baos.toString("utf-8");
    } catch (ParserConfigurationException | TransformerException | SAXException | IOException ex) {
        throw new RuntimeException("Can't pretty print xml!", ex);
    }
}