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 prettyPrint(Node node) {
    try {/*from   w  ww.  ja v a2s  .  c o m*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        return writer.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String writeXmlToStream(Document doc, Result streamResult) {
    String result = "";
    try {//from   w  w w .  j av a 2s  .  c  om
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Write the DOM document to the file
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer xformer = tFactory.newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        xformer.transform(source, streamResult);
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
    }
    return result;
}

From source file:Main.java

/**
 * Creates a new document transformer//w  w  w  .j a  v a 2 s  .  c o  m
 * @return the transformer
 */
public static Transformer createTransformer() throws TransformerConfigurationException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    return transformer;
}

From source file:Main.java

/**
 * Writes the XML document to the particular file specified as argument
 * //from  ww w. java 2 s  . c o m
 * @param doc the document
 * @param filename the path to the file in which to write the XML data writing
 *            operation fails
 */
public static void writeXMLDocument(Document doc, String filename) {
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filename));
        transformer.transform(source, result);
        log.fine("writing operation to " + filename + " successful!");
    } catch (TransformerConfigurationException e) {
        log.warning(e.getMessage());
    } catch (TransformerException e) {
        log.warning(e.getMessage());
    }
}

From source file:Main.java

/**
 * Serialize XML from a {@link Document} to an OutputStream.
 * @param doc// w w  w.  jav  a  2 s .c o m
 * @param out
 */
public static void serialiseXml(Document doc, Writer out) {
    DOMSource domSource = new DOMSource(doc.getDocumentElement());
    StreamResult streamResult = new StreamResult(out);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = null;
    try {
        serializer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
    }
    serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "users.dtd");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    try {
        serializer.transform(domSource, streamResult);
    } catch (TransformerException e) {
    }
}

From source file:Main.java

public static void save(Document document, IFile file, IProgressMonitor monitor)
        throws TransformerException, CoreException, UnsupportedEncodingException {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");

    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    DOMSource ds = new DOMSource(document);
    trans.transform(ds, sr);/*  w  w  w . j a v a2s.  c o m*/
    String xmlString = sw.toString();

    InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));

    if (file.exists()) {
        file.setContents(is, 0, monitor);
    } else {
        file.create(is, true, monitor);
    }
}

From source file:Main.java

public static void saveDocumentToFile(Document xmlDoc, File file)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    DOMSource source = new DOMSource(xmlDoc);
    StreamResult result = new StreamResult(file);
    transformer.transform(source, result);
}

From source file:Main.java

public static void marshalToStream(Element elm, 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 2s .  c om*/
    trans.setOutputProperty(OutputKeys.STANDALONE, "no");

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

From source file:Main.java

public static void flush(Document in, OutputStream out) throws RuntimeException {
    try {/* www  .  ja v  a2s  .  co  m*/
        // Write the content into XML file
        Transformer transformer = transformerFactory.newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "no");

        transformer.transform(new DOMSource(in), new StreamResult(out));
    } catch (TransformerException e) {
        throw new RuntimeException("Exception flush document", e);
    }
}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {/*w w  w . j  a v  a2  s. c o  m*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        return result.getWriter().toString();
    } catch (TransformerException ex) {
        System.err.println("Error: Cannot load xml transformer");
        System.err.println("Cause: " + ex.toString());
        System.exit(1);
        return null;
    }
}