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 printNode(Node node) {
    try {//  w w w .j a  v a 2  s. c  o  m
        // Set up the output transformer
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        // Print the DOM node

        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(node);
        trans.transform(source, result);
        String xmlString = sw.toString();

        System.out.println(xmlString);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void printXml(Document doc, Writer writer) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
}

From source file:Main.java

public static String documentToString(Node document) {
    try {/*from   w  w  w .ja va  2s  .com*/
        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, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(document), new StreamResult(sw));
        return sw.toString().replace("\r\n", "\n");
    } catch (TransformerException e) {
        throw new IllegalAccessError("Couldn't transform document to string");
    }
}

From source file:Main.java

/**
 * This method formats the XML file by omitting the XML Declaration and 
 * creating indentations //  w  ww  . j  av  a2s  .c  o  m
 * @param transformer - transformer that is used to process XML
 * @return a transformer that omits the XML declaration and performs indentations  
 * @author Alex Song
 */
private static Transformer formatXML(Transformer transformer) {
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    return transformer;
}

From source file:Main.java

public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {/*from  w ww .  j a v  a  2s.c  o m*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        throw new RuntimeException(te);
    }
    return sw.toString();
}

From source file:Main.java

public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {//from   w  w  w  . ja va2 s  . c om
        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));
    } catch (TransformerException te) {
        System.err.println("nodeToString Transformer Exception");
    }
    return sw.toString();
}

From source file:Main.java

public static void init() {
    if (isInited) {
        return;/* ww w .ja va  2 s  .  com*/
    }
    try {
        tx = TransformerFactory.newInstance().newTransformer();
        tx.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        tx.setOutputProperty(OutputKeys.INDENT, "no");
        isInited = true;
    } catch (TransformerFactoryConfigurationError | TransformerConfigurationException
            | IllegalArgumentException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String documentToString(Document doc)
        throws TransformerConfigurationException, TransformerException, IOException {
    //return doc.getDocumentElement().toString();

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

    DOMSource src = new DOMSource(doc);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/* w  ww.  jav a  2  s.c o  m*/
        StreamResult sr = new StreamResult(baos);
        transformer.transform(src, sr);

        String result = baos.toString();
        return result;
    } finally {
        baos.close();
    }
}

From source file:Main.java

public static String XMLToString(Document doc) throws TransformerException {
    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.toString();
}

From source file:Main.java

public static String transText(Node doc) throws Exception {
    TransformerFactory transfactory = TransformerFactory.newInstance();
    Transformer transformer = transfactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    Source source = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    transformer.transform(source, result);
    return sw.toString();
}