Example usage for javax.xml.transform OutputKeys OMIT_XML_DECLARATION

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

Introduction

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

Prototype

String OMIT_XML_DECLARATION

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

Click Source Link

Document

omit-xml-declaration = "yes" | "no".

Usage

From source file:org.openmrs.module.metadatasharing.converter.ConverterEngine.java

private static String toXML(Node doc) throws SerializationException {
    try {//from  w w w  .j ava2s . c o m
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        transformer.transform(new DOMSource(doc), result);
        return result.getWriter().toString();
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}

From source file:Main.java

/**
 * Save the XML document to an output stream.
 * //from  w w  w. java 2 s. c  o m
 * @param doc
 *            The XML document to save.
 * @param outStream
 *            The stream to save the document to.
 * @param encoding
 *            The encoding to save the file as.
 * 
 * @throws TransformerException
 *             If there is an error while saving the XML.
 */
public static void save(Node doc, OutputStream outStream, 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");

    // initialize StreamResult with File object to save to file
    Result result = new StreamResult(outStream);
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
}

From source file:Main.java

private static Transformer getThreadedTransformer(final boolean omitXmlDeclaration, final boolean standalone,
        final Map threadMap, final String xslURL) throws TransformerConfigurationException {
    final Thread currentThread = Thread.currentThread();
    Transformer transformer = null;
    if (threadMap != null) {
        transformer = (Transformer) threadMap.get(currentThread);
    }//from   w  ww . jav  a2  s.c  o m
    if (transformer == null) {
        if (xslURL == null) {
            transformer = tFactory.newTransformer(); // "never null"
        } else {
            transformer = tFactory.newTransformer(new StreamSource(xslURL));
        }
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        if (threadMap != null) {
            threadMap.put(currentThread, transformer);
        }
    }
    transformer.setOutputProperty(OutputKeys.STANDALONE, standalone ? "yes" : "no");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
    return transformer;
}

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

/**
 * make content/*w  w w.  ja v a 2  s.  c  o m*/
 * 
 * @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: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);/* w  w w  .j a  v  a 2  s. c  o  m*/

    return bos;
}

From source file:gov.nih.nci.cacis.common.test.TestUtils.java

/**
 * Will get an w3c Node element as a String
 *
 * @param node XML Node// www. ja va2 s  .  c  o  m
 * @return String xml as String
 * @throws javax.xml.transform.TransformerException exception
 */
public static String nodeToString(Node node) throws TransformerException {
    final StringWriter sw = new StringWriter();
    final 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

public static final String indenting(Node rootNode) throws XPathExpressionException, TransformerException {

    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", rootNode,
            XPathConstants.NODESET);

    for (int i = 0; i < nodeList.getLength(); ++i) {
        Node node = nodeList.item(i);
        node.getParentNode().removeChild(node);
    }//from  ww  w  . j  a va  2s  . c  o m

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);

    transformer.transform(new DOMSource(rootNode), streamResult);

    return stringWriter.toString();
}

From source file:br.gov.lexml.parser.documentoarticulado.LexMLUtil.java

public static String formatLexML(String xml) {
    String retorno = null;/*from  w w w .j  a v  a  2  s  .  co m*/
    try {
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

        XPath xPath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document,
                XPathConstants.NODESET);

        for (int i = 0; i < nodeList.getLength(); ++i) {
            Node node = nodeList.item(i);
            node.getParentNode().removeChild(node);
        }

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        StringWriter stringWriter = new StringWriter();
        StreamResult streamResult = new StreamResult(stringWriter);

        transformer.transform(new DOMSource(document), streamResult);

        retorno = stringWriter.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return retorno;

}

From source file:Main.java

/**
 * Transform.//from  www  .ja va 2 s. 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:com.beligum.core.utils.Toolkit.java

public static String xmlToString(Document document, boolean indent) throws Exception {
    if (document == null) {
        return null;
    } else {//from   ww w . j  av a2 s  . c o  m
        try {
            StringWriter sw = new StringWriter();
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

            transformer.transform(new DOMSource(document), new StreamResult(sw));
            return sw.toString();
        } catch (Exception e) {
            throw new Exception("Error converting XML to String", e);
        }
    }
}