Example usage for javax.xml.transform OutputKeys INDENT

List of usage examples for javax.xml.transform OutputKeys INDENT

Introduction

In this page you can find the example usage for javax.xml.transform OutputKeys INDENT.

Prototype

String INDENT

To view the source code for javax.xml.transform OutputKeys INDENT.

Click Source Link

Document

indent = "yes" | "no".

Usage

From source file:Main.java

private static String getW3CXmlFromDoc(Document doc) {
    String xmlString = null;/*  w w  w. j a va2 s . co  m*/
    Transformer transformer;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
        // transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // yes,
        // no
        transformer.setOutputProperty(OutputKeys.INDENT, "no");

        // initialize StreamResult with File object to save to file
        StreamResult xmlStream = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, xmlStream);
        xmlString = xmlStream.getWriter().toString();
    } catch (Exception ex) {
        logger.error(ex.getMessage());
    }

    return xmlString;
}

From source file:info.ajaxplorer.client.http.XMLDocEntity.java

public void toLogger() {
    if (true) {//from   w ww  .  j a va 2 s  .  c o  m
        return;
    }
    try {
        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);

        String xmlString = result.getWriter().toString();
        System.out.println(xmlString);
    } catch (Exception e) {
    }
}

From source file:com.bluexml.side.form.utils.DOMUtil.java

/**
 * Gets a DocumentTransformer object (in fact, a singleton), which is created if non existent.
 * /*w w w  . j av a2  s .  co  m*/
 * @return
 */
public static Transformer getDocumentTransformer() {
    if (documentTransformer == null) {
        try {
            documentTransformer = TransformerFactory.newInstance().newTransformer();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
            return null;
        } catch (TransformerFactoryConfigurationError e) {
            e.printStackTrace();
            return null;
        }
        documentTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
    }
    return documentTransformer;
}

From source file:Main.java

public static String toString(Document doc) {
    try {//  ww w.j  a  v  a 2s . c  om
        StringWriter sw = new StringWriter();
        Source source = new DOMSource(doc);
        Result result = new StreamResult(sw);

        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", new Integer(4));
        Transformer xformer = tf.newTransformer();
        xformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.transform(source, result);
        return sw.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

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

/**
 * make content//from w w w  . j  a  v a2 s. com
 * 
 * @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:nl.igorski.lib.utils.data.XMLTool.java

/**
 * perform a full dump of a XML Document
 * for debugging purposes//from w  w w.j  a  v  a  2  s.c  o m
 *
 * @param doc {Node}
 * @return {String}
 */
public static String nodeToString(Node doc) {
    Transformer transformer = null;

    try {
        transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    } catch (TransformerConfigurationException e) {
    }

    if (transformer != null) {
        //initialize StreamResult with File object to save to file
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);

        try {
            transformer.transform(source, result);
        } catch (TransformerException e) {
        }

        return result.getWriter().toString();
    }
    return null;
}

From source file:Main.java

/**
 * Transform.//from   w  w  w  . j  a  va 2s.c  om
 *
 * @param document
 *          the document
 * @param streamResult
 *          the stream result
 * @param xmlDeclaration
 *          the xml declaration
 */
public static void transform(final Document document, final StreamResult streamResult,
        final boolean xmlDeclaration) {
    if (document == null) {
        throw new IllegalArgumentException("document cannot be NULL!");
    }
    if (streamResult == null) {
        throw new IllegalArgumentException("streamResult cannot be NULL!");
    }
    Transformer transformer;
    try {
        transformer = TRANSFORMER_FACTORY.newTransformer();
    } catch (final TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    String strXmlDeclaration;
    if (xmlDeclaration) {
        strXmlDeclaration = "no";
    } else {
        strXmlDeclaration = "yes";
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, strXmlDeclaration);
    final DOMSource source = new DOMSource(document);
    try {
        transformer.transform(source, streamResult);
    } catch (final TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.github.kahowell.xformsvc.util.TransformUtil.java

public String transform(String transformationName, String source)
        throws TransformerConfigurationException, TransformerException, UnsupportedEncodingException {
    TransformerFactory factory = TransformerFactory.newInstance();
    factory.setURIResolver(getURIResolver());
    Transformer transformer = factory.newTransformer(lookupTransformer(transformationName));
    transformer.setOutputProperty(OutputKeys.INDENT, prettyPrint ? "yes" : "no");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    transformer.transform(new StreamSource(new ByteArrayInputStream(source.getBytes("UTF-8"))),
            new StreamResult(out));
    return out.toString("UTF-8");
}

From source file:Main.java

private static boolean updateXML(Document document, String path) throws TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();

    // out put encoding.
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DocumentType type = document.getDoctype();
    if (type != null) {
        System.out.println("doctype -->" + type.getPublicId());
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, type.getPublicId());
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, type.getSystemId());
    }/*from w w  w  .  j  av a 2s.c  om*/

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(new File(path));
    transformer.transform(source, result);

    transformer.reset();
    return true;
}