Example usage for javax.xml.transform Transformer setOutputProperty

List of usage examples for javax.xml.transform Transformer setOutputProperty

Introduction

In this page you can find the example usage for javax.xml.transform Transformer setOutputProperty.

Prototype

public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;

Source Link

Document

Set an output property that will be in effect for the transformation.

Usage

From source file:Main.java

/**
 * //ww w  .j  a v a 2  s . com
 * @param header Just a title for the stanza for readability.  Single word no spaces since
 * it is inserted as the root element in the output.
 * @param xml The string to pretty print
 */
static public void prettyPrint(String header, String xml) {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");

        if (header != null) {
            xml = "\n<" + header + ">" + xml + "</" + header + '>';
        }
        transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(System.out));
    } catch (Exception e) {
        System.out.println("Something wrong with xml in \n---------------\n" + xml + "\n---------------");
        e.printStackTrace();
    }
}

From source file:Main.java

public static void saveXml(Document dom, File file, boolean omitXmlDeclaration) throws IOException {
    try {/* www. j av a2 s. co  m*/
        Transformer transformer = getNewTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
        transformer.transform(new DOMSource(dom.getDocumentElement()),
                new StreamResult(file.toURI().getPath()));
    } catch (Exception e) {
        throw new IOException("saveXml failed because : " + e.getMessage());
    }
}

From source file:com.syrup.storage.xml.XmlFactory.java

/**
 * Convert document to string. Helper method.
 * /* w ww  .  j a v a2 s.  co m*/
 * @param document
 *            the document object.
 * @return String.
  * @throws java.io.IOException when unable to write the xml
  * @throws javax.xml.transform.TransformerException when unable to transform the document
 */
public static String documentToString(Document document) throws IOException, TransformerException {
    String soapRequest = null;

    if (document != null) {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, HTTP.UTF_8);
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(document);
        transformer.transform(source, result);
        return result.getWriter().toString();
    }

    return soapRequest;
}

From source file:Main.java

/**
 * write this as an XML document in a stream
 * @param out the stream//from  w  w w . ja  v  a2 s .c  om
 * @param omitdecl should we omit the declaration
 */
static public void print(Node node, Writer out, boolean omitdecl) {
    try {
        if (node == null)
            return;

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("omit-xml-declaration", (omitdecl ? "yes" : "no"));
        DOMSource source = new DOMSource(node);
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException("Cannot print Node", e);
    }

}

From source file:Main.java

public static String toString(Element element) {
    try {//from ww  w  . j av  a 2 s . c o m
        Transformer transformer = TransformerFactory.newInstance().newTransformer();

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

        StringWriter buffer = new StringWriter();
        transformer.transform(new DOMSource(element), new StreamResult(buffer));

        return buffer.toString();

    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static String toString(Document doc, String encoding, boolean indent)
        throws TransformerFactoryConfigurationError, TransformerException {
    Source source = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    Result result = new StreamResult(sw);

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.ENCODING, encoding);

    if (indent) {
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    }/*from   w  ww  .  j av  a 2  s  . c  o m*/

    xformer.transform(source, result);

    return sw.getBuffer().toString();
}

From source file:com.mlo55.utils.XMLUtils.java

public static Transformer getTransformer() throws Exception {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(ENCODING, "UTF-8");
    transformer.setOutputProperty(INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(2));
    return transformer;
}

From source file:Main.java

/**
 * @param indentation If greater than zero, then the XML will be indented by the value specified
 *//*from  w  w w .  j  a va  2 s  .  c om*/
private static Transformer getXMLidentityTransformer(int indentation) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer t = factory.newTransformer(); // identity transform
    t.setOutputProperty(OutputKeys.INDENT, (indentation != 0) ? YES : NO);
    t.setOutputProperty(OutputKeys.METHOD, "xml"); //xml, html, text
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "" + indentation);
    return t;
}

From source file:Main.java

public static String writeDocumentToString(Document document) {
    try {//from  w  w  w  . j a v  a 2 s  . c o  m
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans;
        trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        StringWriter writer = new StringWriter();
        try {
            StreamResult streamResult = new StreamResult(writer);
            DOMSource source = new DOMSource(document);
            trans.transform(source, streamResult);
        } catch (TransformerException e) {
            e.printStackTrace();
            return null;
        } finally {
            writer.close();
        }
        return writer.toString();

    } catch (TransformerConfigurationException | IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

/**
 * Save a DOM document//from   w w  w . j av a2 s. c o  m
 */
public static void saveDocument(Document document, OutputStream out, boolean indent) throws IOException {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        if (indent) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        }
        transformer.transform(new DOMSource(document), new StreamResult(out));
    } catch (TransformerException e) {
        throw new IOException(e.getMessage());
    }
}