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:Main.java

public static void transformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer();
    if (omitXmlDeclaration) {
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }// w ww  .j  a v  a2  s  .c  om
    trans.transform(new DOMSource(node), new StreamResult(os));
}

From source file:Main.java

/**
 * Create and returns a new Transformer.
 *///from w ww .j  a  v a  2  s. c om
public static Transformer createTransformer() {
    initFactories();

    final Transformer result;

    try {
        result = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        return null;
    }

    result.setOutputProperty(OutputKeys.METHOD, "xml");
    result.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    result.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    result.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    result.setOutputProperty(OutputKeys.INDENT, "yes");

    return result;
}

From source file:Main.java

public static String prettyXML(String xml, int indent) {
    try {//from   w  w  w .  ja va2 s .  co m
        // Turn xml string into a document
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

        // Remove whitespaces outside tags
        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);
        }
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StringWriter stringWriter = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
        return stringWriter.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String nodeListToString(NodeList nodeList) throws TransformerException {
    StringWriter stringWriter = new StringWriter();
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Node node = nodeList.item(i);
        if (node instanceof Element) {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(new DOMSource(node), new StreamResult(stringWriter));
        } else {/*from   w  ww.j a v a  2 s .  com*/
            stringWriter.append(node.getTextContent());
        }
    }
    return stringWriter.toString();
}

From source file:Main.java

private static String convertDOMToString(DOMSource source, boolean outputXmlDeclaration) {
    StreamResult result = null;/* w  w w .ja v  a 2 s. c om*/
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        if (!outputXmlDeclaration) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }

        result = new StreamResult(new StringWriter());
        transformer.transform(source, result);
    } catch (TransformerException e) {

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

From source file:Main.java

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

    try {/*from w  w  w. ja  v a2  s . com*/
        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 String getXMLString(Document dom, boolean bOmitDeclaration, String sEncoding) {
    String sOmit = (bOmitDeclaration ? "yes" : "no");
    try {// w ww . jav a  2  s .  c  o  m
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        if (sEncoding != null)
            transformer.setOutputProperty(OutputKeys.ENCODING, sEncoding);
        StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, sOmit);
        transformer.transform(new DOMSource(dom), new StreamResult(buffer));
        return buffer.toString();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String documentToString(Document d) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {//from   w w  w. j  av  a2  s  .co  m
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        return null;
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(d), new StreamResult(writer));
    } catch (TransformerException e) {
        e.printStackTrace();
        return null;
    }
    return writer.getBuffer().toString().replaceAll("\n|\r", "");
}

From source file:Main.java

/**
 * Convert an <code>org.w3c.dom.Node</code> to a <code>String</code> without including any XML header information.
 * /*from  w w w . jav  a2s . co m*/
 * @param node the <code>org.w3c.dom.Node</code> to convert to a String.
 * @return a <code>String</code> representing the the <code>org.w3c.dom.Node</code>
 * @throws TransformerException if there was a problem with the conversion
 */
public static String nodeToString(Node node) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

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

    return stringWriter.toString();
}

From source file:Main.java

public static String convertToString(Node node) {
    boolean withXMLDeclaration = true;
    String result;/*from ww  w  .j av  a 2  s.  c  o  m*/
    if (withXMLDeclaration) {
        Document document = node.getOwnerDocument();
        DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
        LSSerializer serializer = domImplLS.createLSSerializer();
        result = serializer.writeToString(node);
    } else {
        try {
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            StringWriter buffer = new StringWriter();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(new DOMSource(node), new StreamResult(buffer));
            result = buffer.toString();
        } catch (TransformerConfigurationException e) {
            result = "";
        } catch (TransformerException e) {
            result = "";
        }
    }
    return result;
}