Example usage for javax.xml.transform OutputKeys METHOD

List of usage examples for javax.xml.transform OutputKeys METHOD

Introduction

In this page you can find the example usage for javax.xml.transform OutputKeys METHOD.

Prototype

String METHOD

To view the source code for javax.xml.transform OutputKeys METHOD.

Click Source Link

Document

method = "xml" | "html" | "text" | expanded name.

Usage

From source file:Main.java

private static String getIndented(Document aDoc) {
    String outputXml = "";
    try {/* www .j  a  v  a 2  s  . c o m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        //      tf.setAttribute("indent-number", new Integer(4));
        Transformer transformer;
        transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(aDoc);
        transformer.transform(source, result);
        outputXml = result.getWriter().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return outputXml;
}

From source file:Main.java

public static void formatDocument(Document document, Writer out, String encoding) throws TransformerException {
    document.normalizeDocument();/*from  w  w  w .  j a v a  2  s  .  c  o  m*/
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer idTransform = transFactory.newTransformer();
    idTransform.setOutputProperty(OutputKeys.METHOD, "xml");
    idTransform.setOutputProperty(OutputKeys.INDENT, "yes");
    if (encoding != null) {
        idTransform.setOutputProperty(OutputKeys.ENCODING, encoding);
    }
    idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    Source source = new DOMSource(document);
    Result result = new StreamResult(out);
    idTransform.transform(source, result);
}

From source file:Main.java

/**
 * Creates a new document transformer//www. j ava  2s . 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

public static void setCommonOutputProperties(final Transformer transformer, final boolean indentOutput)
        throws TransformerConfigurationException {
    transformer.setOutputProperty(OutputKeys.METHOD, XML);
    transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8);
    transformer.setOutputProperty(OutputKeys.VERSION, VERSION);
    if (indentOutput) {
        transformer.setOutputProperty(OutputKeys.INDENT, YES);
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
    } else {/*from  w w  w .ja  v a 2  s.  c o m*/
        transformer.setOutputProperty(OutputKeys.INDENT, NO);
    }
}

From source file:Main.java

public static void writeXMLDocument(Document doc, StreamResult out)
        throws TransformerConfigurationException, TransformerException {
    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();

    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, out);
}

From source file:Main.java

/**
 * Dump a {@link Document} or {@link Node}-compatible object to the given {@link OutputStream} (e.g. System.out).
 *
 * @param _docOrNode {@link Document} or {@link Node} object
 * @param _outStream {@link OutputStream} to print on
 * @throws IOException on error//from   w  ww.j  ava 2  s  .  c  om
 */
public static void printDocument(Node _docOrNode, OutputStream _outStream) throws IOException {
    if (_docOrNode == null || _outStream == null) {
        throw new IOException("Cannot print (on) 'null' object");
    }

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        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(_docOrNode),
                new StreamResult(new OutputStreamWriter(_outStream, "UTF-8")));
    } catch (UnsupportedEncodingException | TransformerException _ex) {
        throw new IOException("Could not print Document or Node.", _ex);
    }

}

From source file:Main.java

/**
 * Pretty format a given XML document/*w  ww.  jav a 2 s  . c  o m*/
 *
 * @param strInput
 *            Valid XML document (No validity check yet!)
 * @param nIndent
 *            Indent
 * @return Formatted XML document
 * @throws Exception
 *             in error case
 */
public static String prettyFormat(String strInput, int nIndent) throws Exception {
    try {
        Source xmlInput = new StreamSource(new StringReader(strInput));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", nIndent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(nIndent));
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Pretty formatting: " + e.getMessage(), LogLevel.Error);
        throw e;
    }
}

From source file:Main.java

public static void printDOM(OutputStream ostr, Document doc, String encoding) {
    try {/*from   ww  w.ja v  a  2s  . c  om*/
        Transformer tr = TransformerFactory.newInstance().newTransformer();
        if (encoding != null)
            tr.setOutputProperty(OutputKeys.ENCODING, encoding);
        tr.setOutputProperty(OutputKeys.INDENT, "yes");
        tr.setOutputProperty(OutputKeys.METHOD, "xml");
        tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        tr.transform(new DOMSource(doc), new StreamResult(ostr));
    } catch (TransformerConfigurationException ex) {
        System.err.println(ex.getMessage());
    } catch (TransformerException ex) {
        System.err.println(ex.getMessage());
    }
}

From source file:Main.java

/**
 * Convert XML DOM document to a XML string representation
 *
 * @param doc// www .  j a  v a  2 s .c  om
 *            XML DOM document
 * @return XML string
 * @throws Exception
 *             in error case
 */
public static String xmlDOMDocumentToString(Document doc) throws Exception {
    if (doc == null) {
        throw new RuntimeException("No XML DOM document (null)!");
    }
    StringWriter stringWriter = new StringWriter();
    String strDoc = null;

    try {
        StreamResult streamResult = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        // transformerFactory.setAttribute("nIndent-number", new Integer(4));
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.transform(new DOMSource(doc.getDocumentElement()), streamResult);
        stringWriter.flush();
        strDoc = stringWriter.toString();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Parsing of XML DOM document failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (stringWriter != null) {
            stringWriter.close();
            stringWriter = null;
        }
    }

    return strDoc;
}

From source file:Main.java

/**
 * Method to get formatted Xml string from Xml source
 *
 * @param payload Source/*from  w  ww . j  ava2 s  .c om*/
 * @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;
}