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

private static void write(Document doc) {
    try {//from  ww w.  ja v a 2  s . c  om
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("hooks.xml"));
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
        transformer.transform(source, result);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String documentToString(Document d) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {// w  w w  . j  av  a2 s .  c om
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        return null;
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(d), new StreamResult(writer));
    } catch (TransformerException e) {
        e.printStackTrace();
        return null;
    }
    return writer.getBuffer().toString().replaceAll("\n|\r", "");
}

From source file:Main.java

public static String prettifyXmlString(String uglyXml) {
    Transformer transformer = null;
    try {//  www . j  a  v  a2 s.  c om
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    }
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    //initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = null;
    try {
        source = new DOMSource(string2Document(uglyXml));
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    String xmlString = result.getWriter().toString();
    return xmlString;
}

From source file:Main.java

public static String createString(Element element) {
    try {//from   www  .  java2  s.  c o  m
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        DOMSource source = new DOMSource(element);
        Writer writer = new StringWriter();
        Result result = new StreamResult(writer);
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, result);
        return writer.toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String docToString(Document doc, boolean formated) {

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/* w w w  . j  a  v a 2 s .  c  om*/
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        return null;
    }

    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    if (formated) {
        // linefeed formatting
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    } else {
        // remove xml header
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);

    DOMSource domSource = new DOMSource(doc);
    try {
        transformer.transform(domSource, sr);
    } catch (TransformerException e) {
        e.printStackTrace();
        return null;
    }
    return sw.toString();
}

From source file:Main.java

public static InputStream createInputStream(Element element) throws IOException {
    byte[] ret = null;
    try {/*from  w w  w . ja v a2  s  . c  o  m*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        DOMSource source = new DOMSource(element);
        Writer writer = new StringWriter();
        Result result = new StreamResult(writer);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, result);
        ret = writer.toString().getBytes("UTF-8");
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return new ByteArrayInputStream(ret);
}

From source file:org.eclipse.swordfish.core.util.xml.XmlUtil.java

private static Transformer getTransformer() {
    if (transformerFactory == null) {
        transformerFactory = TransformerFactory.newInstance();
    }// w  w w. j  a  va 2 s.  co  m
    Assert.notNull(transformerFactory);
    Transformer ret;
    try {
        ret = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException ex) {
        throw new RuntimeException(ex);
    }
    Assert.notNull(ret);
    ret.setOutputProperty(OutputKeys.ENCODING, defaultCharset);
    return ret;
}

From source file:fr.ece.epp.tools.Utils.java

public static void output(Node node, String filename) {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    try {/*from www.jav a2  s  . c  om*/
        Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty("encoding", "utf8");
        transformer.setOutputProperty("indent", "yes");
        DOMSource source = new DOMSource();
        source.setNode(node);
        StreamResult result = new StreamResult();
        if (filename == null) {
            result.setOutputStream(System.out);
        } else {
            result.setOutputStream(new FileOutputStream(filename));
        }
        transformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * @return a transformer that indents entries by 4 characters (never null)
 *///from w w  w.  j a  v  a  2s .c om
public static Transformer createIndentingTransformer() {
    Transformer transformer;
    try {
        TRANSFORMER_FACTORY.setAttribute("indent-number", 4);
        transformer = TRANSFORMER_FACTORY.newTransformer();
    } catch (final Exception e) {
        throw new IllegalStateException(e);
    }
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    return transformer;
}

From source file:Main.java

/**
 * @return a transformer that indents entries by 4 characters (never null)
 *//*from w w  w .  j a v a2 s . c  o  m*/
public static final Transformer createIndentingTransformer() {
    Transformer xformer;
    try {
        transformerFactory.setAttribute("indent-number", 4);
        xformer = transformerFactory.newTransformer();
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    return xformer;
}