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 void serialize(Node n, StreamResult sr) throws IOException, TransformerException {
    TransformerHandler hd = newTransformerHandler();
    Transformer serializer = hd.getTransformer();

    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(n);
    serializer.transform(source, sr);/*  w w  w. j  a  v  a  2 s  .c om*/
}

From source file:Main.java

private static void save(Document document, OutputStream outputStream)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException,
        IOException {/*from ww w.  j  av a  2 s. c om*/
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(outputStream);
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty("indent", "yes");
    transformer.transform(source, result);
    outputStream.close();
}

From source file:Main.java

/** ask transformer to use UTF-8
 * @param transformer will encode output as UTF-8
 *//*from   w w w .  j  a v  a 2s . co m*/
public static void setUtfEncoding(final Transformer transformer) {
    transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
}

From source file:Main.java

/** ask transformer to pretty-print the output
 * @param transformer will indent output
 *///from w ww .j  a  v  a  2s.  com
public static void setIndentFlag(final Transformer transformer) {
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // see http://www.w3.org/TR/xslt#output
}

From source file:Main.java

/** ask transformer to output stand-alone fragment
 * @param transformer will suppress xml declaration
 *//*from   w  ww . j  a  v  a2s  .com*/
public static void outputStandaloneFragment(final Transformer transformer) {
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
}

From source file:Main.java

public static int node2Stream(Node node, OutputStream out) throws Exception {
    if (node == null || out == null)
        return -1;

    try {/*  www .j  a  v  a 2s . c  o  m*/
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.transform(new DOMSource(node), new StreamResult(out));
        return 0;
    } catch (Exception e) {
        throw e;
    }
}

From source file:Main.java

public static String prettyPrintXML(String xml) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult result = new StreamResult(new StringWriter());
    StreamSource source = new StreamSource(new StringReader(xml));
    transformer.transform(source, result);
    return result.getWriter().toString();
}

From source file:Main.java

public static String format(String input, int indent) {
    try {// w w  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);
    }
}

From source file:Main.java

public static synchronized void writeXmlFile(String xmlFile, Document document) throws Exception {
    // transform the document to the file
    TransformerFactory xformFactory = TransformerFactory.newInstance();
    Transformer idTransformer = xformFactory.newTransformer();
    idTransformer.setOutputProperty("indent", "yes");
    Source input = new DOMSource(document);
    Result output = new StreamResult(new File(xmlFile));
    idTransformer.transform(input, output);
    /*// w w w  .  j ava2  s .  c o  m
     OutputFormat format = new OutputFormat();
     format.setLineSeparator(LineSeparator.Unix);
     format.setIndenting(true);
     format.setLineWidth(0);
     format.setPreserveSpace(true);
     XMLSerializer ser = new XMLSerializer(new FileWriter(xmlFile),
     format);
     ser.asDOMSerializer();
     ser.serialize(document);
     */
    return;
}

From source file:Main.java

public static String documentToString(Document doc, String xslName) {
    StringWriter sw = new StringWriter();
    try {//from w  w w .j  a v a  2 s  .c  o  m
        TransformerFactory transfactory = TransformerFactory.newInstance();
        Transformer transformer = transfactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        Source source = new DOMSource(doc.getDocumentElement());
        Result result = new StreamResult(sw);
        transformer.transform(source, result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return head + xslName + sw.toString();
}