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 documentToString(Document document) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;
    try {/*from w  ww .j ava 2  s  .c  o  m*/
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(document), new StreamResult(writer));
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return writer.getBuffer().toString().replaceAll("\n|\r", "");
}

From source file:Main.java

/**
 * Create a {@link Transformer} just the way we like it.
 *
 * @return Returns a new {@link Transformer}.
 *//*from  w w  w  .j av  a2  s.  co m*/
private static Transformer createTransformer() {
    final Transformer xform;
    try {
        xform = m_xformFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new IllegalStateException(e);
    }
    xform.setOutputProperty(OutputKeys.INDENT, "yes");
    xform.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");
    xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    return xform;
}

From source file:com.marklogic.mapreduce.DOMDocument.java

static ByteArrayOutputStream serialize(Node node) throws TransformerException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Result rslt = new StreamResult(bos);
    Source src = new DOMSource(node);
    Transformer transformer = getTransformerFactory().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(src, rslt);//from  w  w  w .java  2s .  c o m

    return bos;
}

From source file:Main.java

public static String documentToString(Document document) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {// w ww . j  a  v  a  2s . co m
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(document), new StreamResult(writer));
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return output;
}

From source file:ee.ria.xroad.common.Request.java

/**
 * Converts the given SOAP message string to a pretty-printed format.
 * @param soap the SOAP XML to convert//from   w ww.jav a 2  s .c o m
 * @return pretty-printed String of the SOAP XML
 */
public static String prettyFormat(String soap) {
    try {
        Source xmlInput = new StreamSource(new StringReader(soap));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String prettyPrint(Document doc) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;
    StringWriter writer = new StringWriter();
    try {//from w ww . j  a v  a  2s  .  co m
        serializer = tfactory.newTransformer();
        //Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //$NON-NLS-1$ //$NON-NLS-2$

        serializer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:PayHost.Utilities.java

/**
 * Make the http post response pretty with indentation
 *
 * @param input Response/*ww  w . jav a 2  s  .  co  m*/
 * @param indent Indentation level
 * @return String in pretty format
 */
public static String prettyFormat(String input, int indent) {
    try {
        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 (IllegalArgumentException | TransformerException e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:com.hangum.tadpole.engine.sql.util.export.XMLExporter.java

/**
 * make content//  w w w .  ja  v  a 2s .  c om
 * 
 * @param tableName
 * @param rsDAO
 * @return
 */
public static String makeContent(String tableName, QueryExecuteResultDTO rsDAO, int intLimitCnt)
        throws Exception {
    final StringWriter stWriter = new StringWriter();
    final List<Map<Integer, Object>> dataList = rsDAO.getDataList().getData();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    final Document doc = builder.newDocument();
    final Element results = doc.createElement(tableName);
    doc.appendChild(results);

    Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName();
    for (int i = 0; i < dataList.size(); i++) {
        Map<Integer, Object> mapColumns = dataList.get(i);
        Element row = doc.createElement("Row");
        results.appendChild(row);

        for (int j = 1; j < mapColumns.size(); j++) {
            String columnName = mapLabelName.get(j);
            String strValue = mapColumns.get(j) == null ? "" : "" + mapColumns.get(j);

            Element node = doc.createElement(columnName);
            node.appendChild(doc.createTextNode(strValue));
            row.appendChild(node);
        }

        if (i == intLimitCnt)
            break;
    }

    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    tf.setAttribute("indent-number", 4);

    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//"ISO-8859-1");
    StreamResult sr = new StreamResult(stWriter);
    transformer.transform(domSource, sr);

    return stWriter.toString();
}

From source file:Main.java

static void emitDocument(Document doc, OutputStream os, String encoding) throws IOException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = null;
    try {/*ww  w  .  j a v  a2  s  . c o  m*/
        t = tf.newTransformer();
        //SF:no dtd            t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, PROPS_DTD_URI);
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.ENCODING, encoding);
    } catch (TransformerConfigurationException tce) {
        assert (false);
    }
    DOMSource doms = new DOMSource(doc);
    StreamResult sr = new StreamResult(os);
    try {
        t.transform(doms, sr);
    } catch (TransformerException te) {
        IOException ioe = new IOException();
        ioe.initCause(te);
        throw ioe;
    }
}

From source file:Main.java

public static void SaveDom(Document dom, String filepath) {
    try {/*from w  w w  .j a v a2s . c o m*/
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        DOMSource domSource = new DOMSource(dom);

        StreamResult streamResult = new StreamResult(new FileOutputStream(filepath));
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.transform(domSource, streamResult);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}