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 writeXmlDocToStream(Document xmlReport, OutputStream stream) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(xmlReport);
    StreamResult result = new StreamResult(stream);
    transformer.transform(source, result);
}

From source file:Main.java

public static void flush(Document in, OutputStream out) throws RuntimeException {
    try {//from   w  ww. jav  a 2s  .com
        // Write the content into XML file
        Transformer transformer = transformerFactory.newTransformer();

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

        transformer.transform(new DOMSource(in), new StreamResult(out));
    } catch (TransformerException e) {
        throw new RuntimeException("Exception flush document", e);
    }
}

From source file:Main.java

public static String DocToString(Document doc)
        throws TransformerFactoryConfigurationError, TransformerException {
    if (doc == null)
        return new String("");
    String xmlString = "";
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    //initialize StreamResult with File object to save to file   
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    xmlString = result.getWriter().toString();

    return xmlString;
}

From source file:Main.java

/**
 * Print the indented document./*from w  w  w.ja v  a 2 s . c  o  m*/
 * 
 * @param stream
 * @param document
 * 
 * @throws UnsupportedEncodingException 
 * @throws TransformerExceptions 
 */
public static void printXMLFormat(Document document) throws UnsupportedEncodingException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 4); //$NON-NLS-1$
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$

    StreamResult streamResult = new StreamResult(); //$NON-NLS-1$

    DOMSource source = new DOMSource(document);
    transformer.transform(source, streamResult);
}

From source file:Main.java

/**
 * Saves the XML file to disk.//from  w w  w  .  j  a va2 s  .  co  m
 * @param fileName The name of the file.
 * @param doc The XML Document to save to disk.
 * @throws TransformerConfigurationException
 * @throws TransformerException 
 */
private static void store(String fileName, Document doc)
        throws TransformerConfigurationException, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    transformer.transform(new DOMSource(doc), new StreamResult(new File(fileName)));
}

From source file:Main.java

public static String getString(Node doc) {
    StringWriter sw = new StringWriter();

    try {/*  ww  w . j av  a 2s .  c o m*/
        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
        trans.transform(new DOMSource(doc), new StreamResult(sw));
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

public static byte[] serializeFragment(Document root)
        throws TransformerException, UnsupportedEncodingException {
    Source source = new DOMSource((Node) root);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");

    return serializeXML(transformer, source);
}

From source file:Main.java

/**
 * Convert the document to an array of bytes.
 *
 * @param doc The XML document.//from  w  w  w.ja  va  2s. co m
 * @param encoding The encoding of the output data.
 *
 * @return The XML document as an array of bytes.
 *
 * @throws TransformerException If there is an error transforming to text.
 */
public static byte[] asByteArray(Document doc, String encoding) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    StringWriter writer = new StringWriter();
    Result result = new StreamResult(writer);
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    return writer.getBuffer().toString().getBytes();
}

From source file:Main.java

private static void transform(Document doc, StreamResult result) throws TransformerException {
    DOMSource source = new DOMSource(doc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
    transformer.transform(source, result);
}

From source file:Main.java

/**
 * Returns the String-Representation of the given DOM-Node as well-formed DOM-Document.
 *
 * @param node DOM-Node to print//ww  w .ja  v  a2s .  c om
 * @param indent if true resulting XML is endented
 * @return <code>String</code> - Node as XML-String
 * @throws Exception on error
 */
public static String domNode2String(Node node, boolean indent) throws Exception {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");

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

    String xmlString = result.getWriter().toString();

    return xmlString;
}