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

/**
 * Serialize an XML document into a String (for debug purpose only!). the
 * returned String could contains an error message instead if a problem as
 * occurred during the serialization. This method is intended for debug /
 * trace purpose because you really don't need to serialize XML at all in
 * your code unless your planing to output it to a file. In this case, use
 * {@link XmlHelper#writeXmlToFile(Document, File)} instead.
 *///from   w  ww .jav  a  2  s  .  c  om
public static String dumpXml(Document document) {
    TransformerFactory factory = TransformerFactory.newInstance();
    try {
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException e) {
        return "XML dump failed: " + e.getMessage();
    }
}

From source file:Main.java

/**
 * Convert the document to an array of bytes.
 * /* w  w w .  ja va2 s.  c  o m*/
 * @param doc
 *            The XML document.
 * @param encoding
 *            The encoding of the output data.
 * 
 * @return The XML document as an array of bytes.
 * 
 * @throws TransformerException
 *             If there is an error transforming to text.
 */
public static byte[] asByteArray(Document doc, String encoding) throws TransformerException {
    final Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    final StringWriter writer = new StringWriter();
    final Result result = new StreamResult(writer);
    final DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    return writer.getBuffer().toString().getBytes();
}

From source file:Main.java

/**
 * Method for static initializer/*from ww  w. j ava  2s .  co m*/
 */
private static Properties createDefaultPropertiesForXML(boolean omitXMLDeclaration) {
    final Properties format = new Properties();
    format.put(OutputKeys.METHOD, "xml");
    format.put(OutputKeys.OMIT_XML_DECLARATION, (omitXMLDeclaration ? "yes" : "no"));
    format.put(OutputKeys.INDENT, "yes");
    return format;
}

From source file:Main.java

static public String getPrettyPrint(Document doc) {
    try {// w w w .  jav  a  2  s  .  c  om
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * Save the XML document to a file./*from w  w  w .  j  a v a  2  s  .  co  m*/
 *
 * @param doc The XML document to save.
 * @param file The file 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(Document doc, String file, String encoding) throws TransformerException {
    try {
        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(new File(file));
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
    } finally {

    }
}

From source file:Main.java

static void formatXMLFile(String file) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new InputStreamReader(new FileInputStream(file))));

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.METHOD, "xml");
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.setOutputProperty("b;http://xml.apache.org/xsltd;indent-amount", "4");
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    Source source = new DOMSource(document);
    Result result = new StreamResult(new File(file));
    xformer.transform(source, result);//  ww w .  j a v  a  2s .  com
}

From source file:Main.java

private static String nodeToString(Node node) throws Exception {
    StringWriter sw = new StringWriter();

    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.transform(new DOMSource(node), new StreamResult(sw));

    return sw.toString();
}

From source file:Main.java

public static String docToString(Document doc, boolean formated) {

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {//  ww  w . j  av a2 s  .  c om
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        return null;
    }

    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    if (formated) {
        // linefeed formatting
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    } else {
        // remove xml header
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);

    DOMSource domSource = new DOMSource(doc);
    try {
        transformer.transform(domSource, sr);
    } catch (TransformerException e) {
        e.printStackTrace();
        return null;
    }
    return sw.toString();
}

From source file:Main.java

public static StringBuffer transformToString(Source xmlSource, Source xslSource) {
    StringWriter writer = new StringWriter();
    Transformer transformer;/*from w  w  w.  j a  v  a  2 s  . c om*/
    try {
        if (xslSource == null) {
            transformer = TransformerFactory.newInstance().newTransformer();
        } else {
            transformer = TransformerFactory.newInstance().newTransformer(xslSource);
        }
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(xmlSource, new StreamResult(writer));
        return writer.getBuffer();
    } catch (Exception e) {
        e.printStackTrace();
        return writer.getBuffer();
    } finally {
        try {
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static String getXML(NodeList childNodes) {
    try {/*from www . j  a v  a  2 s .c  om*/
        StringBuilder builder = new StringBuilder();
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        for (int i = 0; i < childNodes.getLength(); i++) {
            StringWriter sw = new StringWriter();
            t.transform(new DOMSource(childNodes.item(i)), new StreamResult(sw));
            builder.append(sw.toString());
        }
        return builder.toString();
    } catch (Exception ex) {
        return "";
    }
}