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 writeDocument(Document document, File directoryToWriteTo, String fileName) {
    // Prepare the DOM document for writing
    Source source = new DOMSource(document);

    // Prepare the output file
    File file = new File(directoryToWriteTo, fileName);
    Result result = new StreamResult(file);

    // Write the DOM document to the file
    try {//from ww  w  .  ja va  2s . co m
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(source, result);
    } catch (TransformerFactoryConfigurationError | TransformerException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

/**
 * Save the XML document to a file./*from   w ww  .j av  a2 s . co  m*/
 *
 * @param doc The XML document to save.
 * @param file The file to save the document to.
 * @param encoding The encoding to save the file as.
 *
 * @throws TransformerException If there is an error while saving the XML.
 */
public static void save(Document doc, String file, String encoding) throws TransformerException {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        //        initialize StreamResult with File object to save to file

        Result result = new StreamResult(new File(file));
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
    } finally {

    }
}

From source file:Main.java

private static void SaveCustomerFile(Document CustomerDoc, JFrame mainFrame) {
    try {/*from  www  .j a v  a  2  s . c  o m*/
        TransformerFactory Factory = TransformerFactory.newInstance();
        Transformer Trans = Factory.newTransformer();
        DOMSource source = new DOMSource(CustomerDoc);
        File f = new File(".");
        String FilePath = f.getAbsoluteFile().getParent() + "\\Customers.xml";
        f = new File(FilePath);
        if (f.exists()) {
            f.delete();
        }
        StreamResult Result = new StreamResult(f);
        Trans.setOutputProperty(OutputKeys.INDENT, "yes");
        Trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "7");
        Trans.transform(source, Result);
    } catch (TransformerException ex) {
        System.out.println(ex.getMessage());
        JOptionPane.showMessageDialog(mainFrame,
                "There Was an Error Saving The File. Please Restart the Application.");
        System.exit(1);
    }
}

From source file:Main.java

public static byte[] documentToByteArray(Document data, Integer indent) throws TransformerException {
    ByteArrayOutputStream result = new ByteArrayOutputStream();

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    if (indent != null) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", indent.toString());
    }//from w w w . jav a 2 s . c om
    transformer.transform(new DOMSource(data), new StreamResult(result));
    return result.toByteArray();
}

From source file:Main.java

public static void saveXml(Document modDoc, String path) throws TransformerException, IOException {
    Writer writer = null;/*  www. j av  a  2  s  .co  m*/
    try {
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();
        Source src = new DOMSource(modDoc);
        writer = getFileWriter(path);
        Result dest = new StreamResult(writer);

        aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
        aTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
        aTransformer.transform(src, dest);
    } catch (TransformerException e) {
        throw e;
    } finally {

        writer.close();
    }

}

From source file:Main.java

public static String printXMLNode(Node node) {
    try {/*from  w  w w .  jav  a 2  s  .  co m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", 2);
        Transformer transformer = tf.newTransformer();
        String omitDeclaration = node instanceof Document
                || node == node.getOwnerDocument().getDocumentElement() ? "no" : "yes";
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        output = removeEmptyLines(output); // .replaceAll("\n|\r", "");
        return output;
    } catch (TransformerException te) {
        throw new RuntimeException(te);
    }
}

From source file:Main.java

static void formatXMLFile(String file) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new InputStreamReader(new FileInputStream(file))));

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.METHOD, "xml");
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.setOutputProperty("b;http://xml.apache.org/xsltd;indent-amount", "4");
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    Source source = new DOMSource(document);
    Result result = new StreamResult(new File(file));
    xformer.transform(source, result);/* ww  w.ja  v a  2s  . co m*/
}

From source file:Main.java

/** Write an XML DOM tree to a file
 *
 * @param doc the document to write// ww w.  j  a  v a 2s.c  o  m
 * @param file the file to write to
 * @throws IOException if a file I/O error occurs
 */
public static void write(Document doc, File file) throws IOException {
    try {
        DOMSource domSource = new DOMSource(doc);
        FileOutputStream stream = new FileOutputStream(file);
        // OutputStreamWriter works around indent bug 6296446 in JDK
        // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446
        StreamResult streamResult = new StreamResult(new OutputStreamWriter(stream));
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", new Integer(2));
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.transform(domSource, streamResult);
    } catch (TransformerException e) {
        // This exception is never thrown, treat as fatal if it is
        throw new RuntimeException(e);
    }
}

From source file:Main.java

static public void toHtml(Document xmldoc, OutputStream os) throws TransformerException {
    StreamResult out = new StreamResult(os);
    DOMSource domSource = new DOMSource(xmldoc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "html");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, out);
}

From source file:Main.java

/**
 * Formats a xml string//from  ww  w .  j av a2 s.co m
 * @param input
 * @param indent
 * @return
 */
public static String prettyFormatXml(String input, int indent) {
    try {
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);

        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return input;
}