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 serializeXML(Node e, Writer out) throws Exception {
    DOMSource domSource = new DOMSource(e);
    StreamResult streamResult = new StreamResult(out);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    // turn off <?xml...?> stuff as for documents that were parsed with
    // non-UTF8 encoding, serializer inserts encoding="[non-utf-8]" there which
    // it should not, since we always serialize as UTF-8
    serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    // serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.transform(domSource, streamResult);
}

From source file:Main.java

public static String formatXmlAsString(Document document) {

    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();

    try {//w w  w. j  a  v a2  s . c o m

        factory.setAttribute("indent-number", new Integer(2));
    } catch (IllegalArgumentException ex) {

    }

    try {

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

        transformer.transform(new DOMSource(document), new StreamResult(writer));
    } catch (TransformerException ex) {

        throw new RuntimeException("Error formatting xml as pretty-printed string", ex);
    }

    return writer.toString();
}

From source file:Main.java

/**
 * DOM to string./*from  w  ww  . j  a  v  a 2  s  .  c  om*/
 *
 * @param doc
 *            the doc
 * @return the string
 */
/*
 * from:
 * http://www.journaldev.com/71/utility-java-class-to-format-xml-document
 * -to-xml-string-and-xml-to-document
 */
public static String DOMToString(Document doc) {
    String xmlString = "";
    if (doc != null) {
        try {
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            xmlString = sw.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return xmlString;
}

From source file:Main.java

/**
 * Save document to a file//  w w  w.  j  a v a 2s .  c o  m
 *
 * @param document : Document to be saved
 * @param fileName : Represent file name to save
 * @throws Exception
 */
public static void saveDocumentTo(Document document, String fileName) throws Exception {
    File encryptionFile = new File(fileName);
    try (FileOutputStream fOutStream = new FileOutputStream(encryptionFile)) {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(fOutStream);
        transformer.transform(source, result);
    }
}

From source file:Main.java

public static String getXMLString(Element elm, boolean htmlMode) throws TransformerException {
    /* TODO: Certain HTML entities, like "&#8658;", currently seem impossible to represent, as the
         HTML mode will output them in a named form unsupported by the Swing HTML parser
         (e.g. "&lArr;"). */// w  w  w . j ava 2  s.  c  o m
    Transformer t;
    t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.METHOD, htmlMode ? "html" : "xml");
    t.setOutputProperty(OutputKeys.INDENT, "no");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, htmlMode ? "yes" : "no");
    StringWriter ret = new StringWriter();
    t.transform(new DOMSource(elm), new StreamResult(ret));
    return ret.toString();
}

From source file:Main.java

/**  This method dumps out a dom document to an output stream.
 *   Header information is turned off./*  w  w  w  . j  a v  a 2 s .  c o m*/
 *
 *   @param doc Dom Document
 *   @param os existing outputstream you wish to write the document to.
 */
public static void DOMtoOutputStream(Document doc, OutputStream os) {
    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        DOMSource src = new DOMSource(doc);
        StreamResult result = new StreamResult(os);

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

        //transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.transform(src, result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void saveReportToFile(Node node, String documentFileName) {
    // StringWriter writer = new StringWriter();
    // StreamResult resultString = new StreamResult(writer);

    File output = new File(documentFileName);
    StreamResult resultFile = new StreamResult(output);

    DOMSource source = new DOMSource(node);
    try {/*from   w  ww. java2s .  c o  m*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        t.transform(source, resultFile);
        // t.transform(source, resultString);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    // return writer.toString();
}

From source file:Main.java

public static String writeDocumentToString(Document document) {
    try {/* w  ww.j av  a 2 s  .  co m*/
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans;
        trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        StringWriter writer = new StringWriter();
        try {
            StreamResult streamResult = new StreamResult(writer);
            DOMSource source = new DOMSource(document);
            trans.transform(source, streamResult);
        } catch (TransformerException e) {
            e.printStackTrace();
            return null;
        } finally {
            writer.close();
        }
        return writer.toString();

    } catch (TransformerConfigurationException | IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static String printXMLNode(Node node) {
    try {//from w ww  .java 2 s .c om
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", 2);
        Transformer transformer = tf.newTransformer();
        String omitDeclaration = node instanceof Document
                || node == node.getOwnerDocument().getDocumentElement() ? "no" : "yes";
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        output = removeEmptyLines(output); // .replaceAll("\n|\r", "");
        return output;
    } catch (TransformerException te) {
        throw new RuntimeException(te);
    }
}

From source file:Main.java

private static String toString(final Document document, final boolean indent, final boolean omitXmlDeclaration)
        throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
    final Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
    transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

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

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

    return stringWriter.toString();
}