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:eu.eidas.engine.test.simple.SSETestUtils.java

/**
 * Prints the tree DOM./*from   w  ww.j av  a2 s .co m*/
 *
 * @param samlToken the SAML token
 * @param isIndent the is indent
 *
 * @return the string
 * @throws TransformerException the exception
 */
public static String printTreeDOM(final Element samlToken, final boolean isIndent) throws TransformerException {
    // set up a transformer
    final TransformerFactory transfac = TransformerFactory.newInstance();
    final Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    trans.setOutputProperty(OutputKeys.INDENT, String.valueOf(isIndent));

    // create string from XML tree
    final StringWriter stringWriter = new StringWriter();
    final StreamResult result = new StreamResult(stringWriter);
    final DOMSource source = new DOMSource(samlToken);
    trans.transform(source, result);
    final String xmlString = stringWriter.toString();

    return xmlString;
}

From source file:gov.nih.nci.cabig.caaers.utils.pdf.MedwatchUtils.java

public static String evalXPathsOnNode(Node n, String strXPath) {
    try {//from  www  .  j  a  v a  2 s. c  o  m

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        DOMSource domSource = new DOMSource(n);
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        transformer.transform(domSource, result);
        return evalXPathOnXML(sw.toString(), strXPath);

    } catch (Exception e) {
        log.debug(e);
    }

    return "";
}

From source file:Main.java

/**
 * Transform document to string representation.
 *
 * @param document/*from  ww  w .j a  v  a2 s .co m*/
 *            XHTML document
 * @return string representation of document
 * @throws TransformerException
 *             if it is not possible to create a Transformer instance or to transform document
 */
public static String toString(final Document document) throws TransformerException {
    final StringWriter stringWriter = new StringWriter();
    final StreamResult streamResult = new StreamResult(stringWriter);
    final Transformer transformer = TRANSFORMER_FACTORY.get().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, TRANSFORMER_YES);
    // set indent for XML
    transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");
    // not all JavaSE have the same implementation
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    // transformer.setOutputProperty(OutputKeys.METHOD, "html");
    // html method transforms <br/> into <br>, which cannot be re-parsed
    // transformer.setOutputProperty(OutputKeys.METHOD, "xhtml");
    // xhtml method does not work for my xalan transformer
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, TRANSFORMER_YES);
    transformer.transform(new DOMSource(document.getDocumentElement()), streamResult);
    return stringWriter.toString();
}

From source file:XmlUtils.java

public static void styleDocument(Document document, String stylesheet, boolean xslInPath,
        Map<String, Object> parameters, OutputStream out) throws Exception {

    Transformer transformer = XmlUtils.getTransformer(stylesheet, xslInPath);
    if (parameters != null) {
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            transformer.setParameter(entry.getKey(), entry.getValue());
        }//from w w  w .  ja  v a2 s  . com
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    DocumentSource source = new DocumentSource(document);
    // now lets style the given document

    StreamResult sresult = new StreamResult(out);
    transformer.transform(source, sresult);

}

From source file:com.cisco.dvbu.ps.common.adapters.util.XmlUtils.java

public static String nodeToString(Node node) throws Exception {
    StringWriter sw = new StringWriter();
    try {//from w w  w . j av  a  2 s.  co m
        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) {
        log.error("Xml to String Transformation error!" + te.getMessage());
        throw te;
    }
    return sw.toString();
}

From source file:IOUtils.java

public static ContentHandler getSerializer(File file) throws IOException, TransformerException {
    final FileWriter writer = new FileWriter(file);

    final TransformerHandler transformerHandler = FACTORY.newTransformerHandler();
    final Transformer transformer = transformerHandler.getTransformer();

    final Properties format = new Properties();
    format.put(OutputKeys.METHOD, "xml");
    format.put(OutputKeys.OMIT_XML_DECLARATION, "no");
    format.put(OutputKeys.ENCODING, "UTF-8");
    format.put(OutputKeys.INDENT, "yes");
    transformer.setOutputProperties(format);

    transformerHandler.setResult(new StreamResult(writer));

    try {//from w  ww  . j ava  2  s  .  c o m
        if (needsNamespacesAsAttributes(format)) {
            return new NamespaceAsAttributes(transformerHandler);
        }
    } catch (SAXException se) {
        throw new TransformerException("Unable to detect of namespace support for sax works properly.", se);
    }
    return transformerHandler;
}

From source file:Main.java

/**
 * Serialize an XML Element into a String.
 * @param df DocumentFragment to be serialized.
 * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration.
 * @return String representation of the XML document fragment.
 *///from   w  w w. ja v  a 2 s .  c  o m
public static final String serialize(DocumentFragment df, boolean omitXMLDeclaration) {
    if (df != null) {
        try {
            DOMSource domSource = new DOMSource(df);
            Transformer serializer = TransformerFactory.newInstance().newTransformer();
            serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                    ((omitXMLDeclaration) ? "yes" : "no"));
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING);
            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            serializer.transform(domSource, new StreamResult(baos));
            baos.close();
            return new String(baos.toByteArray(), UTF8_ENCODING);
        } catch (Throwable t) {
        }
    }
    return null;
}

From source file:Main.java

/** ask transformer to output stand-alone fragment
 * @param transformer will suppress xml declaration
 *//*from  www . j  ava  2s. c  o m*/
public static void outputStandaloneFragment(final Transformer transformer) {
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
}

From source file:com.thinkbiganalytics.feedmgr.nifi.NifiTemplateParser.java

public static String documentToString(Document doc) throws Exception {

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    String output = writer.getBuffer().toString();
    return output;
}

From source file:org.opencastproject.remotetest.util.Utils.java

/**
 * Converts the node to a string representation.
 * //from   ww  w .ja  va 2s.c om
 * @param node
 *          the node
 * @return the string representation
 * @throws Exception
 */
public static String nodeToString(Node node) throws Exception {
    DOMSource domSource = new DOMSource(node);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(out);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(domSource, result);
    InputStream in = new ByteArrayInputStream(out.toByteArray());
    return IOUtils.toString(in, "UTF-8");
}