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 getDomDocumentAsXml(org.w3c.dom.Document domDocument) {
    try {// w  ww.j a  v  a  2s  .c o  m
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult xmlOutput = new StreamResult(new StringWriter());
        transformer.transform(new DOMSource(domDocument), xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static void write(Document doc) {
    try {/* w  w  w.  ja  v  a2s. c  o  m*/
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("hooks.xml"));
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
        transformer.transform(source, result);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String transformXmlToString(Document doc, String encoding)
        throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();

    Document document = builder.newDocument();

    Element e = doc.getDocumentElement();
    Node n = document.importNode(e, true);
    document.appendChild(n);//from   w  ww  .  j  a  v  a 2s  .  c om

    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    DOMSource source = new DOMSource(document);

    StringWriter sw = new StringWriter();

    StreamResult result = new StreamResult(sw);

    transformer.transform(source, result);

    return sw.toString();
}

From source file:Main.java

public static void prettyPrintXml(Document doc, OutputStream out, int indent) {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    TransformerFactory tfactory = TransformerFactory.newInstance();

    // set indent spaces on factory
    //        tfactory.setAttribute("indent-number", new Integer(indent));

    try {//  w w w . ja  v a  2 s  .  c o  m
        Transformer serializer = tfactory.newTransformer();

        // turn on indenting on transformer (serializer)
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
                //                  String.valueOf(indent));
                "2");

        serializer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "utf-8")));
    } catch (Exception e) {

        // this is fatal, just dump stack and throw runtime exception
        e.printStackTrace();

        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void exportXmlFile(ArrayList<?> listObject, String rootElement, String interfaceName,
        String pathSaveFile) {/*from   w w w . j  a  v a  2s  . c o  m*/
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
        // creating a new instance of a DOM to build a DOM tree.
        Document doc = docBuilder.newDocument();

        Element root = doc.createElement(rootElement);
        // adding a node after the last child node of the specified node.
        doc.appendChild(root);

        Element interfaceElement = null;

        Element child = null;
        Text text;

        for (Object obj : listObject) {
            Class srcClass = obj.getClass();
            Field[] field = srcClass.getFields();
            interfaceElement = doc.createElement(interfaceName);
            for (int i = 0; i < field.length; i++) {
                // System.out.println(field[i].getName() + ":" +
                // field[i].get(obj));
                child = doc.createElement(field[i].getName());

                text = doc.createTextNode((field[i].get(obj)).toString());
                child.appendChild(text);

                interfaceElement.appendChild(child);
            }
            root.appendChild(interfaceElement);
        }

        // TransformerFactory instance is used to create Transformer
        // objects.
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

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

        // create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        String xmlString = sw.toString();

        File file = new File(pathSaveFile);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        bw.write(xmlString);
        bw.flush();
        bw.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Pretty prints the document to string using specified charset.
 * @param document the document//w w  w . ja  va 2s  .c o m
 * @param charset the charset
 * @return printed document in String form
 * @throws Exception if any errors occur
 */
public static String prettyPrintXml(Document document, String charset) throws Exception {
    StringWriter stringWriter = new StringWriter();
    StreamResult output = new StreamResult(stringWriter);

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, charset);
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.transform(new DOMSource(document), output);

    return output.getWriter().toString().trim();
}

From source file:Executable.ExtendedXMLConfiguration.java

/**
 * Copied from internet so who knows (hence placeholders)!
 * @return Placeholder// w ww .  j  av  a 2s  .c om
 * @throws ConfigurationException Placeholder
 */
@Override
protected Transformer createTransformer() throws ConfigurationException {
    Transformer transformer = super.createTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    return transformer;
}

From source file:Main.java

/**
 * Transform this {@link Node} into a {@link String} representation.
 *
 * @param xml//from ww  w .  j ava 2s. c  o m
 *        the xml Node
 * @return a String representation of the given xml Node
 */
public static String toString(Node xml) {
    short type = xml.getNodeType();

    if (type == Node.TEXT_NODE)
        return xml.getNodeValue();

    StringWriter buffer = new StringWriter();
    try {
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(xml), new StreamResult(buffer));
    } catch (IllegalArgumentException | TransformerException e) {
        //throw new XMLHandlingException("Cannot create String from Node '" + xml + "'.");
        return "";
    }
    return buffer.toString();
}

From source file:Main.java

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

From source file:Main.java

public static void writeXml(Document doc, File file) {
    try {/* w  w  w .  j  a  va  2 s .  c om*/
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

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

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();

        try {
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        } catch (IllegalArgumentException e) { // ignore
        }

        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        throw new IllegalStateException(e);
    } catch (TransformerException e) {
        throw new IllegalStateException(e);
    }
}