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 transText(Node doc) throws Exception {
    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);
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    transformer.transform(source, result);
    return sw.toString();
}

From source file:Main.java

public static String toXMLString(Document doc) throws TransformerException {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");
    // create string from xml tree
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(doc);
    trans.transform(source, result);/*from  ww w  .j a  v a  2 s  . c  o m*/
    String xmlString = sw.toString();
    return xmlString;
}

From source file:Main.java

/** Write an XML DOM tree to a file
 *
 * @param doc the document to write//from   w  w  w. j  av a2s  .c o m
 * @param file the file to write to
 * @throws IOException if a file I/O error occurs
 */
public static void write(Document doc, File file) throws IOException {
    try {
        DOMSource domSource = new DOMSource(doc);
        FileOutputStream stream = new FileOutputStream(file);
        // OutputStreamWriter works around indent bug 6296446 in JDK
        // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446
        StreamResult streamResult = new StreamResult(new OutputStreamWriter(stream));
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", new Integer(2));
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.transform(domSource, streamResult);
    } catch (TransformerException e) {
        // This exception is never thrown, treat as fatal if it is
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String getDocumentAsXml(Document doc) {
    StringWriter sw = new java.io.StringWriter();
    try {/*www . j a va 2s  .c  om*/
        DOMSource domSource = new DOMSource(doc);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult sr = new StreamResult(sw);
        transformer.transform(domSource, sr);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

public static String elementToString(final Node node) throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final Document document = factory.newDocumentBuilder().newDocument();
    final Node importedNode = document.importNode(node, true);
    document.appendChild(importedNode);/*w  ww .  ja  v  a 2 s .  com*/
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(bos));
    return bos.toString("UTF-8");
}

From source file:Main.java

public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException {
    if (node == null)
        return "";
    StringWriter sw = new StringWriter();
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

private static Transformer getTransformer() throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    return transformer;
}

From source file:Main.java

/**
 * Saves the document in the given file/*from   w  w  w  . jav  a2s.  c o m*/
 * @param modelDoc
 * @param fileName
 * @throws Exception
 */
public static void saveDocument(Document doc, File file) throws Exception {
    if (doc == null || file == null)
        return;

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

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

From source file:Main.java

public static void saveXML(Document paramDocument, File paramFile) throws TransformerException {
    TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
    Transformer localTransformer = localTransformerFactory.newTransformer();
    localTransformer.setOutputProperty("indent", "yes");
    StreamResult localStreamResult = new StreamResult(paramFile);
    DOMSource localDOMSource = new DOMSource(paramDocument);
    localTransformer.transform(localDOMSource, localStreamResult);
}

From source file:Main.java

public static String documentToString(Document doc)
        throws TransformerConfigurationException, TransformerException, IOException {
    //return doc.getDocumentElement().toString();

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

    DOMSource src = new DOMSource(doc);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from   w w  w  . j  a va  2s  . c o m*/
        StreamResult sr = new StreamResult(baos);
        transformer.transform(src, sr);

        String result = baos.toString();
        return result;
    } finally {
        baos.close();
    }
}