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

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

From source file:Main.java

public static void transformDOMToFile(Node node, String filePath)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    Source source = new DOMSource(node);
    File file = new File(filePath);
    Result result = new StreamResult(file);

    transformer.transform(source, result);
}

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);/*w  w  w  .j a  v  a 2  s .com*/

}

From source file:Main.java

public static String xmlToString(Node node) {
    try {//from  www .ja v a 2s  . c  om
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String getStringFromXmlDoc(org.w3c.dom.Node node) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.getBuffer().toString().replaceAll("\n|\r", "");
}

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

public static String serializeDocument(Document document) throws Exception {
    // Serialize XML document to String.
    StringWriter writer = new StringWriter();
    StreamResult streamResult = new StreamResult(writer);

    DOMSource domSource = new DOMSource(document);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.transform(domSource, streamResult);

    return writer.toString();
}

From source file:Main.java

public static void saveDoc(Document docToSave, String filePath) throws TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    DOMSource source = new DOMSource(docToSave);
    StreamResult result = new StreamResult(new File(filePath));
    transformer.transform(source, result);
}

From source file:Main.java

public static String parse(Document document) {
    try {/*  w  w  w.j a  va2s  .  c  o m*/
        TransformerFactory dbf = TransformerFactory.newInstance();
        Transformer t = dbf.newTransformer();
        t.setOutputProperty("encoding", "utf-8");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        t.transform(new DOMSource(document), new StreamResult(bos));
        return bos.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void printDocument(Node doc, OutputStream out) {

    try {/*  www.j  a  v a2 s.c om*/
        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")));
    } catch (Exception e) {
        e.printStackTrace();
    }
}