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

/**
 * Pretty prints the document to string using specified charset.
 * @param document the document/*ww w  .j  a  v a 2s.c om*/
 * @param charset the charset
 * @return printed document in String form
 * @throws Exception if any errors occur
 */
public static String prettyPrintXml(Document document, String charset) throws Exception {
    StringWriter stringWriter = new StringWriter();
    StreamResult output = new StreamResult(stringWriter);

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, charset);
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.transform(new DOMSource(document), output);

    return output.getWriter().toString().trim();
}

From source file:Main.java

public static final String createStringFromDOMNode(Node node, boolean omitDeclaration) {
    assert node != null;

    StringWriter out = null;/*from www .ja v  a2  s .c  o  m*/
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");

        node.normalize();

        Source source = new DOMSource(node);
        out = new StringWriter();
        Result resultStream = new StreamResult(out);

        if (omitDeclaration) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        } else {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        }

        transformer.transform(source, resultStream);
    } catch (Exception e) {
    }

    if (out != null) {
        return out.toString();
    }

    return null;
}

From source file:Main.java

public static void printStream(InputStream is) {
    StreamSource source = new StreamSource(is);
    String msgString = null;/*from w  w w. j av  a 2s . c  om*/
    try {
        Transformer xFormer = TransformerFactory.newInstance().newTransformer();
        xFormer.setOutputProperty("omit-xml-declaration", "yes");
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        Result result = new StreamResult(outStream);
        xFormer.transform(source, result);
        outStream.writeTo(System.out);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public final static void applyXSLTTransform(final Reader xslt, final Reader input, final Writer output)
        throws TransformerException {
    final Transformer transformer = transformerFactory.newTransformer(new StreamSource(xslt));

    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, OUTPUT_PROPERTY_YES);
    transformer.setOutputProperty(OutputKeys.INDENT, OUTPUT_PROPERTY_NO);

    transformer.transform(new StreamSource(input), new StreamResult(output));
}

From source file:Main.java

public static byte[] documentToByteArray(Document data, Integer indent) throws TransformerException {
    ByteArrayOutputStream result = new ByteArrayOutputStream();

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    if (indent != null) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", indent.toString());
    }/*from  w w w.  ja  v a 2  s .  c o m*/
    transformer.transform(new DOMSource(data), new StreamResult(result));
    return result.toByteArray();
}

From source file:Main.java

/**
 * Parse file to xml type/* ww w  . jav  a2 s  .c om*/
 *
 * @param transformer
 * @param source
 * @param file
 * @throws FileNotFoundException
 * @throws TransformerException
 */
public static void parseFileToXML(Transformer transformer, DOMSource source, File file)
        throws FileNotFoundException, TransformerException {
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    PrintWriter pw = new PrintWriter(file);
    StreamResult streamResult = new StreamResult(pw);
    transformer.transform(source, streamResult);
}

From source file:Main.java

public static void writeDocument(Document doc, OutputStream os) throws IOException {
    try {//from   w  w  w. j  a  v  a 2s.c o  m
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");

        DOMSource source = new DOMSource(doc);
        CharsetEncoder utf8Encoder = Charset.forName("UTF-8").newEncoder();
        StreamResult result = new StreamResult(new OutputStreamWriter(os, utf8Encoder));
        transformer.transform(source, result);
    } catch (TransformerException e) {
        System.err.println(e.getMessage());
    }
}

From source file:Main.java

public static String getXmlString(Document xmlDoc)
        throws TransformerFactoryConfigurationError, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    //initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(xmlDoc);
    transformer.transform(source, result);

    return result.getWriter().toString();
}

From source file:Main.java

public static String getXmlString(Node xmlNode)
        throws TransformerFactoryConfigurationError, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    //initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(xmlNode);
    transformer.transform(source, result);

    return result.getWriter().toString();
}

From source file:Main.java

static void emitDocument(Document doc, OutputStream os, String encoding) throws IOException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = null;
    try {/*from  www  . j av  a  2 s .  c o m*/
        t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, PROPS_DTD_URI);
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.ENCODING, encoding);
    } catch (TransformerConfigurationException tce) {
        assert (false);
    }
    DOMSource doms = new DOMSource(doc);
    StreamResult sr = new StreamResult(os);
    try {
        t.transform(doms, sr);
    } catch (TransformerException te) {
        IOException ioe = new IOException();
        ioe.initCause(te);
        throw ioe;
    }
}