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

public static String DocumentToStr(Document doc) throws TransformerException {

    StringWriter sw = new StringWriter();
    TransformerFactory tf = TransformerFactory.newInstance();
    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.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();

}

From source file:Main.java

public static void transform(Document src, Writer dst, String xsl) throws TransformerException {

    DOMSource dsrc = new DOMSource(src);
    StreamResult res = new StreamResult(dst);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer(new StreamSource(xsl));
    t.setOutputProperty(OutputKeys.METHOD, "xml");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.transform(dsrc, res);/*  www . j a va  2 s .c  o m*/

}

From source file:Main.java

public static void printDOM(Node node, OutputStream out, String encoding) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;/*from  ww  w .  j a  va2s .c o  m*/
    transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(node), new StreamResult(out));
}

From source file:Main.java

public static void printXml(Document xml, Writer out)
        throws TransformerException, UnsupportedEncodingException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    transformer.transform(new DOMSource(xml), new StreamResult(out));
}

From source file:Main.java

static public void toXml(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, "xml");
    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

public static String getXMLString(Element elm, boolean htmlMode) throws TransformerException {
    /* TODO: Certain HTML entities, like "⇒", currently seem impossible to represent, as the
         HTML mode will output them in a named form unsupported by the Swing HTML parser
         (e.g. "⇐"). *//*from www. ja va 2 s .c  o m*/
    Transformer t;
    t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.METHOD, htmlMode ? "html" : "xml");
    t.setOutputProperty(OutputKeys.INDENT, "no");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, htmlMode ? "yes" : "no");
    StringWriter ret = new StringWriter();
    t.transform(new DOMSource(elm), new StreamResult(ret));
    return ret.toString();
}

From source file:Main.java

public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    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(out, "UTF-8")));
}

From source file:Main.java

/**
 * Transform xml Document to String//www . ja  v  a  2  s.  c  o  m
 * @param doc Xml Document
 * @return String representation
 */
public static String getContent(Document doc) {
    try {
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        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.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
    } catch (Exception ex) {
        throw new RuntimeException("Error converting to String", ex);
    }
}

From source file:Main.java

public static String toStringFromDoc(Element elem) {
    String result = null;// w  w  w .  j av a2 s.c om

    StringWriter strWtr = new StringWriter();
    StreamResult strResult = new StreamResult(strWtr);
    TransformerFactory tfac = TransformerFactory.newInstance();
    try {
        javax.xml.transform.Transformer t = tfac.newTransformer();
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html,
        // text
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        t.transform(new DOMSource(elem), strResult);
    } catch (Exception e) {
        System.err.println("XML.toString(Document): " + e);
    }
    result = strResult.getWriter().toString();
    try {
        strWtr.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}

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);
}