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

public static String prettyFormat(String input, int indent) {
    try {/*from  ww w  .j a v a 2s.  c  o  m*/
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:Main.java

public static String toStringFromDoc(Element elem) {
    String result = null;//from  w w  w  .ja va2  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

/**
 * Method to convert document to string.
 * /*from  w ww  . j a  v a2 s. c  o m*/
 * @param doc
 * @return document content as string
 */
public static String doc2String(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 getStringFromNode(Node node) throws Exception {

    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    String xml = writer.toString();

    return xml;/* www .ja  v  a  2  s.co  m*/
}

From source file:Main.java

/**
 * Writes  XML Document into an xml file.
 * /*from   w  w w  .j a  v a  2 s  .  c  om*/
 * @param fileName  the target file with the full path
 * @param document   the source document
 * @return   boolean true if the file saved
 * @throws Exception
 */
public static boolean writeXmlFile(String fileName, Document document) throws Exception {

    // creating and writing to xml file  

    File file = new File(fileName);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); // to prevent XML External Entities attack

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.transform(new DOMSource(document), new StreamResult(file));

    return true;

}

From source file:Main.java

public static void writeTransformedXml(Document doc, OutputStream output, InputStream style)
        throws TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    StreamSource stylesource = new StreamSource(style);
    Transformer transformer = transformerFactory.newTransformer(stylesource);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(output);
    transformer.transform(source, result);
}

From source file:Main.java

public static void writeXmlFile(Document doc, File file)
        throws TransformerConfigurationException, TransformerException {
    Source source = new DOMSource(doc);
    Result result = new StreamResult(file);

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.transform(source, result);
}

From source file:Main.java

public static void saveReportToFile(Node node, String documentFileName) {
    // StringWriter writer = new StringWriter();
    // StreamResult resultString = new StreamResult(writer);

    File output = new File(documentFileName);
    StreamResult resultFile = new StreamResult(output);

    DOMSource source = new DOMSource(node);
    try {//from   w  ww. j a  v a2 s . c o m
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        t.transform(source, resultFile);
        // t.transform(source, resultString);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    // return writer.toString();
}

From source file:Main.java

public static void format(Source source, boolean omitDeclaration, Result result) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    try {//  w w  w  . j  a v  a  2 s  .c  o m
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    } catch (IllegalArgumentException exc) {
        // not supported
    }

    if (omitDeclaration) {
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }
    transformer.transform(source, result);
}

From source file:Main.java

private static void save(Document doc, Result result) {
    //--- Write the XML document in XML file
    try {//from  w  w  w  . ja  va2s. c  o m
        Source source = new DOMSource(doc);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        //--- Transform the DOM document into XML file 
        transformer.transform(source, result);
    } catch (TransformerException e) {
        throw new RuntimeException("XML error : Cannot save : TransformerException", e);
    } catch (TransformerFactoryConfigurationError e) {
        throw new RuntimeException("XML error : Cannot save : TransformerFactoryConfigurationError", e);
    }
}