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

/**
 * Save the XML document to a file.//  ww w . java2s.c  om
 *
 * @param doc The XML document to save.
 * @param file The file to save the document to.
 * @param encoding The encoding to save the file as.
 *
 * @throws TransformerException If there is an error while saving the XML.
 */
public static void save(Document doc, String file, String encoding) throws TransformerException {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        //        initialize StreamResult with File object to save to file

        Result result = new StreamResult(new File(file));
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
    } finally {

    }
}

From source file:Main.java

/**
 * Save the XML document to an output stream.
 *
 * @param doc The XML document to save./*from   w w  w . j  av  a 2  s  .com*/
 * @param outStream The stream to save the document to.
 * @param encoding The encoding to save the file as.
 *
 * @throws TransformerException If there is an error while saving the XML.
 */
public static void save(Node doc, OutputStream outStream, String encoding) throws TransformerException {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        // initialize StreamResult with File object to save to file
        Result result = new StreamResult(outStream);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
    } finally {

    }
}

From source file:com.redhat.akashche.wixgen.cli.Launcher.java

private static void transformWithXsl(String inputPath, String xslPath, String outputPath) throws Exception {
    File file = new File(outputPath);
    if (file.exists())
        throw new IOException("Output file already exists: [" + outputPath + "]");
    TransformerFactory factory = TransformerFactory.newInstance();
    Source xsl = new StreamSource(new File(xslPath));
    Transformer transformer = factory.newTransformer(xsl);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    Source text = new StreamSource(new File(inputPath));
    transformer.transform(text, new StreamResult(new File(outputPath)));
}

From source file:Main.java

public static String transformXmlToString(Document doc, String encoding)
        throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();

    Document document = builder.newDocument();

    Element e = doc.getDocumentElement();
    Node n = document.importNode(e, true);
    document.appendChild(n);//from w  ww.  j a v  a  2  s .c  o m

    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    DOMSource source = new DOMSource(document);

    StringWriter sw = new StringWriter();

    StreamResult result = new StreamResult(sw);

    transformer.transform(source, result);

    return sw.toString();
}

From source file:Main.java

public static String getDocumentAsString(Node node, boolean prettyXml)
        throws TransformerException, UnsupportedEncodingException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();

    if (prettyXml) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    }//from   w  ww  .  j a va2 s  . c o  m
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(node), new StreamResult(baos));

    return baos.toString("UTF-8");
}

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 . j ava  2s  . c o  m*/
        transformer.setOutputProperty(OutputKeys.INDENT, NO);
    }
}

From source file:com.thinkbiganalytics.feedmgr.nifi.NifiTemplateParser.java

public static String documentToString(Document doc) throws Exception {

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    String output = writer.getBuffer().toString();
    return output;
}

From source file:Main.java

private static String docToString(Document doc) throws Exception {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new StringWriter());
    transformer.transform(source, result);

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

From source file:Main.java

public static String getXMLString(Document dom, boolean bOmitDeclaration, String sEncoding) {
    String sOmit = (bOmitDeclaration ? "yes" : "no");
    try {// w  ww. j  a  v a 2 s .c o  m
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        if (sEncoding != null)
            transformer.setOutputProperty(OutputKeys.ENCODING, sEncoding);
        StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, sOmit);
        transformer.transform(new DOMSource(dom), new StreamResult(buffer));
        return buffer.toString();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * convert node object into String object.
 * @param node//from  www .j av a2  s . c  o m
 * @param omitXmlDecl
 * @return
 */
public static String dom2String(Node node, boolean omitXmlDecl) {
    Source source = new DOMSource(node);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        if (omitXmlDecl)
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return stringWriter.getBuffer().toString();
}