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

public static String getXML(NodeList childNodes) {
    try {/*w  w w .  java 2s . c om*/
        StringBuilder builder = new StringBuilder();
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        for (int i = 0; i < childNodes.getLength(); i++) {
            StringWriter sw = new StringWriter();
            t.transform(new DOMSource(childNodes.item(i)), new StreamResult(sw));
            builder.append(sw.toString());
        }
        return builder.toString();
    } catch (Exception ex) {
        return "";
    }
}

From source file:Main.java

/**
 * Pretty print an XML. Use with caution as this drains the source if it is
 * a StreamSource.//from ww  w .  j a v a2 s  . co  m
 * @param source the XML source
 * @return a String with readable XML
 */
public static String prettyPrint(final Source source) {
    try {
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StringWriter writer = new StringWriter();
        serializer.transform(source, new StreamResult(writer));
        return writer.toString();
    } catch (TransformerException e) {
        return e.getMessage();
    }
}

From source file:Main.java

public static File saveToFile(File file, Document document) throws TransformerException {
    // Prepare the DOM document for writing
    Source source = new DOMSource(document);

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

    // Write the DOM document to the file
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.transform(source, result);//from   w  w  w  . j  av a2s .co m

    return file;
}

From source file:Main.java

public synchronized static void writeXml(Document document, Writer writer, String encoding)
        throws TransformerFactoryConfigurationError, TransformerException {
    if (document != null && writer != null) {
        Source source = new DOMSource(document);
        Result result = new StreamResult(writer);
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.ENCODING,
                (encoding == null || encoding.isEmpty()) ? "UTF-8" : encoding);
        xformer.transform(source, result);
    }//  w  w w. jav  a2  s  .  c  o  m
}

From source file:Main.java

public static String xml2Str(Document document) {

    try {//from  ww  w  . jav a 2 s  .c o  m
        DOMSource source = new DOMSource(document);
        StringWriter writer = new StringWriter();
        Result result = new StreamResult(writer);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(source, result);
        return (writer.getBuffer().toString());

    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

From source file:Main.java

/**
 * Method to get formatted Xml string from Xml source
 *
 * @param payload Source/*w w w. ja v  a2s.co  m*/
 * @return a formatted Xml string
 */
public static String getXmlStringFromSource(Source payload) {
    String result = null;
    StreamResult strResult = new StreamResult(new StringWriter());

    if (payload != null) {
        try {
            TransformerFactory factory = TransformerFactory.newInstance();
            //factory.setAttribute("indent-number", new Integer(2));
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1");
            transformer.transform(payload, strResult);
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        result = strResult.getWriter().toString();
    }
    return result;
}

From source file:Main.java

public static File saveToFile(String filename, Document document) throws TransformerException {
    // Prepare the DOM document for writing
    Source source = new DOMSource(document);

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

    // Write the DOM document to the file
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.transform(source, result);/*from  w  w w .  j  a  v  a2s.  c o m*/

    return file;
}

From source file:Main.java

public static void printDocument(Document doc) {
    try {/*from  w  ww  .  j a  v  a2  s . c o  m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(doc),
                new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Convert an <code>org.w3c.dom.Node</code> to a <code>String</code> without including any XML header information.
 * // w  ww .  j  av a 2s .  c  o  m
 * @param node the <code>org.w3c.dom.Node</code> to convert to a String.
 * @return a <code>String</code> representing the the <code>org.w3c.dom.Node</code>
 * @throws TransformerException if there was a problem with the conversion
 */
public static String nodeToString(Node node) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    StringWriter stringWriter = new StringWriter();
    transformer.transform(new DOMSource(node), new StreamResult(stringWriter));

    return stringWriter.toString();
}

From source file:Main.java

public static void document2OutputStream(Document document, String encode, OutputStream os)
        throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty("encoding", encode);
    transformer.setOutputProperty("indent", "yes");

    transformer.transform(source, new StreamResult(os));
}