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 toXMLString(final DOMSource value) {
    final StringWriter writer = new StringWriter();
    try {//w ww  .jav a  2 s. c o  m
        final Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(value, new StreamResult(writer));
    } catch (final Throwable t) {
        //todo throw flash error
        throw new RuntimeException(t);
    }

    final String result = writer.toString();

    return result.substring(0, result.length() - 1);
}

From source file:Main.java

private static Transformer getTransformer() throws TransformerConfigurationException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    // Formatting XML properly
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    return transformer;
}

From source file:Main.java

/**
 * Serialize an XML Element into a String.
 * @param e Element to be serialized.//  w  w w .  j  a v a2  s . com
 * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration.
 * @return String representation of the XML document fragment.
 */
public static final String serialize(Element e, boolean omitXMLDeclaration) {
    if (e != null) {
        try {
            DOMSource domSource = new DOMSource(e);
            Transformer serializer = TransformerFactory.newInstance().newTransformer();
            serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                    ((omitXMLDeclaration) ? "yes" : "no"));
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING);
            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            serializer.transform(domSource, new StreamResult(baos));
            baos.close();
            return new String(baos.toByteArray(), UTF8_ENCODING);
        } catch (Throwable t) {
        }
    }
    return null;
}

From source file:Main.java

/**
 * Generates a XML string from DOM./*from   w ww  .  j a v  a  2 s .  c  om*/
 * 
 * @param xmlDocument the DOM
 * @return XML string
 */
public static String build(Document xmlDocument) throws TransformerException {
    if (transformer == null) {
        TransformerFactory xformFactory = TransformerFactory.newInstance();
        try {
            xformFactory.setAttribute("indent-number", new Integer(4));
        } catch (IllegalArgumentException e) {
            // ignore
        }
        transformer = xformFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    }
    StringWriter out = new StringWriter();
    transformer.transform(new DOMSource(xmlDocument), new StreamResult(out));
    return out.getBuffer().toString();
}

From source file:Main.java

private static void save(Document doc, Result result) {
    //--- Write the XML document in XML file
    try {//from  w w w .  j  av  a 2  s  .co  m
        Source source = new DOMSource(doc);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        //--- Transform the DOM document into XML file 
        transformer.transform(source, result);
    } catch (TransformerException e) {
        throw new RuntimeException("XML error : Cannot save : TransformerException", e);
    } catch (TransformerFactoryConfigurationError e) {
        throw new RuntimeException("XML error : Cannot save : TransformerFactoryConfigurationError", e);
    }
}

From source file:Main.java

public final static void applyXSLTTransform(final Reader xslt, final Reader input, final Writer output)
        throws TransformerException {
    final Transformer transformer = transformerFactory.newTransformer(new StreamSource(xslt));

    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, OUTPUT_PROPERTY_YES);
    transformer.setOutputProperty(OutputKeys.INDENT, OUTPUT_PROPERTY_NO);

    transformer.transform(new StreamSource(input), new StreamResult(output));
}

From source file:Main.java

public static InputStream createInputStream(Element element) throws IOException {
    byte[] ret = null;
    try {/*from w ww  .  ja v  a 2s.  c o  m*/
        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.INDENT, "yes");
        transformer.transform(source, result);
        ret = writer.toString().getBytes("UTF-8");
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return new ByteArrayInputStream(ret);
}

From source file:Main.java

public static void saveDomSource(final DOMSource source, final File target, final File xslt)
        throws TransformerException, IOException {
    TransformerFactory transFact = TransformerFactory.newInstance();
    transFact.setAttribute("indent-number", 2);
    Transformer trans;//from w  w w.  ja  va 2s .c  o  m
    if (xslt == null) {
        trans = transFact.newTransformer();
    } else {
        trans = transFact.newTransformer(new StreamSource(xslt));
    }
    trans.setOutputProperty(OutputKeys.INDENT, "yes");
    FileOutputStream fos = new FileOutputStream(target);
    trans.transform(source, new StreamResult(new OutputStreamWriter(fos, "UTF-8")));
    fos.close();
}

From source file:Main.java

/**
 * Create and returns a new Transformer.
 *//*  w ww . j a v a 2s .co  m*/
public static Transformer createTransformer() {
    initFactories();

    final Transformer result;

    try {
        result = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        return null;
    }

    result.setOutputProperty(OutputKeys.METHOD, "xml");
    result.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    result.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    result.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    result.setOutputProperty(OutputKeys.INDENT, "yes");

    return result;
}

From source file:Main.java

static void emitDocument(Document doc, OutputStream os, String encoding) throws IOException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = null;/*w  w  w.  ja v  a 2s  . c  om*/
    try {
        t = tf.newTransformer();
        //SF:no dtd            t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, PROPS_DTD_URI);
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.ENCODING, encoding);
    } catch (TransformerConfigurationException tce) {
        assert (false);
    }
    DOMSource doms = new DOMSource(doc);
    StreamResult sr = new StreamResult(os);
    try {
        t.transform(doms, sr);
    } catch (TransformerException te) {
        IOException ioe = new IOException();
        ioe.initCause(te);
        throw ioe;
    }
}