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 String toString(Node node) {
    final Transformer identityTransformer;
    try {/*  w  ww.j a  v  a2  s  . c o m*/
        identityTransformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException("Failed to create identity transformer to serialize Node to String", e);
    }
    identityTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    final StringWriter outputWriter = new StringWriter();
    final StreamResult outputTarget = new StreamResult(outputWriter);
    final DOMSource xmlSource = new DOMSource(node);
    try {
        identityTransformer.transform(xmlSource, outputTarget);
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to convert Node to String using Transformer", e);
    }

    return outputWriter.toString();
}

From source file:Main.java

public static String toString(Node node) {
    DOMSource domSource = new DOMSource(node);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    try {//from w  w  w .j  a v a 2s  .  c o m
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static final void writeMalformedXml(Transformer transformer, OutputStream outputEntry, NodeList nodes) {
    Assert.notNull(transformer, "Transformer required");
    Assert.notNull(outputEntry, "Output entry required");
    Assert.notNull(nodes, "NodeList required");

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

    try {/*  w ww .  j  a  v  a  2 s.  c  o  m*/
        for (int i = 0; i < nodes.getLength(); i++) {
            transformer.transform(new DOMSource(nodes.item(i)), createUnixStreamResultForEntry(outputEntry));
        }
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:Main.java

public static String toXMLString(final DOMSource value) {
    final StringWriter writer = new StringWriter();
    try {/*from  w  ww .j  a v a2s.  co  m*/
        final Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(value, new StreamResult(writer));
    } catch (final Throwable t) {
        //todo throw flash error
        throw new RuntimeException(t);
    }

    final String result = writer.toString();

    return result.substring(0, result.length() - 1);
}

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

public static String xmlToString(Document doc) {
    StringWriter sw = new StringWriter();
    try {/*from   w  w w  . j ava2  s  . co  m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING);
        transformer.transform(new DOMSource(doc), new StreamResult(sw));
    } catch (TransformerException e) {
        throw new IllegalArgumentException(e);
    }
    return sw.toString();
}

From source file:Main.java

public static Transformer getTransformer(boolean standalone, boolean indent, int indentNumber,
        boolean omitXmlDeclaration) throws TransformerException {
    TransformerFactory f = TransformerFactory.newInstance();
    f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
    if (indent) {
        f.setAttribute("indent-number", indentNumber);
    }//from  w ww  .  j  a  va2  s . co m

    Transformer t = f.newTransformer();
    if (standalone) {
        t.setOutputProperty(OutputKeys.STANDALONE, "yes");
    }
    if (indent) {
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty("{xml.apache.org/xslt}indent-amount", "" + indentNumber);
    }
    if (omitXmlDeclaration) {
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }

    return t;
}

From source file:Main.java

public static byte[] serializeToByteArray(Document doc) throws IOException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/*from w w  w. j a va2  s.co m*/
        transformer = tFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new IOException("Unable to serialize XML document");
    }
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    String encoding = doc.getInputEncoding();
    if (encoding == null)
        encoding = "UTF-8";
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    DOMSource source = new DOMSource(doc);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Result result = new StreamResult(out);
    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        throw new IOException("Unable to serialize XML document");
    }

    return out.toByteArray();
}

From source file:Main.java

public static void printDocument(Document doc, OutputStream out, boolean prettyPrint)
        throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

    if (prettyPrint) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    }//from w w  w  .  ja va2  s  . com

    transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

From source file:Main.java

/**
 * convert node object into String object.
 * @param node//from  w  w  w.  ja v a2 s  . c  o m
 * @param omitXmlDecl
 * @return
 */
public static String dom2String(Node node, boolean omitXmlDecl) {
    Source source = new DOMSource(node);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        if (omitXmlDecl)
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return stringWriter.getBuffer().toString();
}

From source file:scala.c24.demo.java.C24DemoUtils.java

public static String getDocumentAsString(Resource resource) throws Exception {

    Document document = getDocument(resource);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(document), new StreamResult(writer));
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return output;
}