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:com.zacwolf.commons.wbxcon.WBXCONorg.java

/**
 * This is a helper method that returns a String form of a {@link org.w3c.dom.Document}
 * object, fully indented into a "pretty print" format.
 * @param doc   {@link org.w3c.dom.Document} to be parsed
 * @param out   OutputStream to copy the text into
 * @throws IOException/*from www .  jav  a2  s. c om*/
 * @throws TransformerException
 */
final static void documentPrettyPrint(final Document doc, final OutputStream out)
        throws IOException, TransformerException {
    final Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, org.apache.http.Consts.UTF_8.displayName());
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.transform(new DOMSource(doc), new StreamResult(out));
}

From source file:gov.va.ds4p.ds4pmobileportal.ui.eHealthExchange.java

private String converXmlDocToString(Document xmlDocument) {

    String xmlString = "";

    try {//from   ww  w. ja  v  a  2  s.  c  om
        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");

        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(xmlDocument), new StreamResult(writer));
        xmlString = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return xmlString;
}

From source file:com.maxl.java.aips2xml.Aips2Xml.java

static String prettyFormat(String input) {
    try {/*from  w w  w.j a v  a 2  s . c om*/
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:eu.eidas.auth.engine.SAMLEngineUtils.java

private static String domnodeToString(Node node) throws TransformerException {
    StringWriter buf = new StringWriter();
    Transformer xForm = TransformerFactory.newInstance().newTransformer();
    xForm.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    xForm.transform(new DOMSource(node), new StreamResult(buf));
    return (buf.toString());
}

From source file:be.fedict.eid.idp.common.saml2.Saml2Util.java

/**
 * Convert specified DOM {@link Node} to a string representation
 * /*from   w  ww. j  a v a 2s .  c o  m*/
 * @param domNode
 *            the DOM node
 * @param indent
 *            indent or not
 * @return the string representation of the DOM node
 */
public static String domToString(Node domNode, boolean indent) {

    try {
        Source source = new DOMSource(domNode);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
        transformer.transform(source, result);

        return stringWriter.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.marklogic.client.functionaltest.BasicJavaClientREST.java

/**
 * Convert xml document to string. Useful for debugging purpose
 * @param readContent/*from   ww w .j  av a  2s.c  om*/
 * @return
 * @throws TransformerException
 */
public String convertXMLDocumentToString(Document readContent) 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(readContent), new StreamResult(writer));
    String output = writer.getBuffer().toString();
    return output;
}

From source file:com.marklogic.client.functionaltest.BasicJavaClientREST.java

/**
 * Convert inputsource to string. Used on InputSourceHandle
 * @param fileRead/*from w  w w  . ja v  a2s. co m*/
 * @return
 * @throws IOException 
 * @throws TransformerException 
 */
public String convertInputSourceToString(InputSource fileRead) throws IOException, TransformerException {
    SAXSource saxsrc = new SAXSource(fileRead);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(saxsrc, new StreamResult(writer));
    String output = writer.getBuffer().toString();
    return output;
}

From source file:com.verisign.epp.codec.gen.EPPUtil.java

/**
 * Converts an <code>aElement</code> to a <code>String</code> for printing.
 * //from  w ww  .  j  a v  a2 s.  c o m
 * @param aElement
 *            <code>Element</code> to print
 * @return <code>String</code> representation of <code>Element</code> if
 *         successful; "ERROR" <code>String</code> otherwise.
 */
public static String toString(Element aElement) {
    String ret = "ERROR";
    ByteArrayOutputStream theBuffer = new ByteArrayOutputStream();

    // Serialize DOM Document to stream
    try {
        TransformerFactory transFac = TransformerFactory.newInstance();
        Transformer trans = transFac.newTransformer();
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.transform(new DOMSource(aElement), new StreamResult(theBuffer));
        theBuffer.close();
        ret = new String(theBuffer.toByteArray());
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return ret;
}

From source file:com.verisign.epp.codec.gen.EPPUtil.java

/**
 * Converts an <code>aElement</code> to a <code>String</code> for printing 
 * without pretty printing./*from ww  w  . j a v  a  2  s .  c  om*/
 * 
 * @param aElement
 *            <code>Element</code> to print
 * @return <code>String</code> representation of <code>Element</code> if
 *         successful; "ERROR" <code>String</code> otherwise.
 */
public static String toStringNoIndent(Element aElement) {
    String ret = "ERROR";
    ByteArrayOutputStream theBuffer = new ByteArrayOutputStream();

    // Serialize DOM Document to stream
    try {
        TransformerFactory transFac = TransformerFactory.newInstance();
        Transformer trans = transFac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.transform(new DOMSource(aElement), new StreamResult(theBuffer));
        theBuffer.close();
        ret = new String(theBuffer.toByteArray());
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return ret;
}

From source file:eu.eidas.auth.engine.core.stork.StorkExtensionProcessor.java

private String computeSimpleValue(XSAnyImpl xsAny) {
    if (null != xsAny) {
        List<XMLObject> unknownXMLObjects = xsAny.getUnknownXMLObjects();
        if (null != unknownXMLObjects && !unknownXMLObjects.isEmpty()) {
            try {
                TransformerFactory transFactory = TransformerFactory.newInstance();
                Transformer transformer = transFactory.newTransformer();
                transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                StringWriter stringWriter = new StringWriter();
                transformer.transform(new DOMSource(unknownXMLObjects.get(0).getDOM()),
                        new StreamResult(stringWriter));
                return stringWriter.toString();
            } catch (TransformerConfigurationException e) {
                LOG.warn(SAML_EXCHANGE, "ERROR : transformer configuration exception", e);
            } catch (TransformerException e) {
                LOG.warn(SAML_EXCHANGE, "ERROR :  transformer exception", e);
            }//w ww. jav  a2 s.  co m
        }
        return xsAny.getTextContent();
    }
    return null;
}