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:org.apache.syncope.core.logic.init.CamelRouteLoader.java

private String nodeToString(final Node content, final TransformerFactory tf) {
    String output = StringUtils.EMPTY;

    try {//from  www.  j a  v a  2  s  .  c o m
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(content), new StreamResult(writer));
        output = writer.getBuffer().toString();
    } catch (TransformerException e) {
        LOG.debug("While serializing route node", e);
    }

    return output;
}

From source file:org.apache.syncope.installer.utilities.FileSystemUtils.java

public static void writeXML(final Document doc, final OutputStream out)
        throws IOException, TransformerException {
    final TransformerFactory factory = TransformerFactory.newInstance();
    factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
    final Transformer transformer = factory.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(doc),
            new StreamResult(new OutputStreamWriter(out, Charset.forName("UTF-8"))));
    out.close();/*w w w  .  ja v a  2 s  .c  om*/
}

From source file:org.apache.sysml.conf.DMLConfig.java

public synchronized String serializeDMLConfig() throws DMLRuntimeException {
    String ret = null;//  w  ww .j a v  a2  s.  com
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        //transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(_xmlRoot);
        transformer.transform(source, result);
        ret = result.getWriter().toString();
    } catch (Exception ex) {
        throw new DMLRuntimeException("Unable to serialize DML config.", ex);
    }

    return ret;
}

From source file:org.apache.xml.security.samples.encryption.Decrypter.java

private static void outputDocToFile(Document doc, String fileName) throws Exception {
    File encryptionFile = new File(fileName);
    FileOutputStream f = new FileOutputStream(encryptionFile);

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(f);
    transformer.transform(source, result);

    f.close();/*from   w w w .  ja  va  2s  . c om*/
    System.out.println("Wrote document containing decrypted data to " + encryptionFile.toURL().toString());
}

From source file:org.apache.xml.security.samples.encryption.Encrypter.java

private static void outputDocToFile(Document doc, String fileName) throws Exception {
    File encryptionFile = new File(fileName);
    FileOutputStream f = new FileOutputStream(encryptionFile);

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(f);
    transformer.transform(source, result);

    f.close();/*  w  w  w.j  a  v a  2s . co  m*/
    System.out.println("Wrote document containing encrypted data to " + encryptionFile.toURL().toString());
}

From source file:org.apache.xml.security.test.c14n.implementations.Canonicalizer20010315Test.java

/**
 * This method takes the input bytes as XML Document and converts it to an
 * UTF-16 encoded XML document which is serialized to byte[] and returned.
 *
 * @param input//  w w  w .  j  av  a  2  s.  c om
 *
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
public static byte[] convertToUTF16(byte input[]) throws ParserConfigurationException, IOException,
        SAXException, TransformerConfigurationException, TransformerException {

    //String ENCODING_ISO8859_1 = "ISO-8859-1";
    //String ENCODING_UTF8 = "UTF-8";
    String ENCODING_UTF16 = "UTF-16";
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new ByteArrayInputStream(input));
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING_UTF16);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

    DOMSource source = new DOMSource(doc);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(os);

    transformer.transform(source, result);

    return os.toByteArray();
}

From source file:org.apache.xml.security.test.external.org.apache.xalan.XPathAPI.XalanBug1425Test.java

/**
 * Process input args and execute the XPath.
 *
 * @param xmlString/*from   w  w w.  j ava 2s . c o  m*/
 * @param xpath
 *
 * @throws Exception
 */
static private boolean containsDocumentElement(String xmlString, String xpath) throws Exception {

    DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dfactory.newDocumentBuilder();
    Document doc = db.parse(new ByteArrayInputStream(xmlString.getBytes()));

    // Set up an identity transformer to use as serializer.
    Transformer serializer = TransformerFactory.newInstance().newTransformer();

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

    // Use the simple XPath API to select a nodeIterator.
    // System.out.println("Querying DOM using " + xpath);
    NodeIterator nl = XPathAPI.selectNodeIterator(doc, xpath);

    // Serialize the found nodes to System.out.
    // System.out.println("<output>");
    Node n;

    while ((n = nl.nextNode()) != null) {

        // System.out.println("<node" + ++i + " nodeType=\"" + nodeTypeString[n.getNodeType()] + "\">");
        // serializer.transform(new DOMSource(n), new StreamResult(System.out));
        // System.out.println("</node" + i + ">");
        // System.out.println();
        if (n == doc.getDocumentElement()) {
            return true;
        }
    }

    // System.out.println("</output>");
    return false;
}

From source file:org.automagic.deps.doctor.editor.PomWriterImpl.java

@Override
public byte[] saveChanges(boolean omitXmlDeclaration) {
    try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        if (omitXmlDeclaration) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }//w w  w. jav a  2s. c o m
        Result result = new StreamResult(output);
        Source input = new DOMSource(document);
        transformer.transform(input, result);
        return output.toByteArray();
    } catch (TransformerFactoryConfigurationError | TransformerException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.carewebframework.common.XMLUtil.java

/**
 * Converts an XML document to a formatted XML string.
 *
 * @param doc The document to format./*from  w w  w.j av a 2s.com*/
 * @param indent Number of characters to indent.
 * @return Formatted XML document.
 */
public static String toString(Document doc, int indent) {
    if (doc == null) {
        return "";
    }

    try {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();

        try {
            tf.setAttribute("indent-number", indent);
        } catch (IllegalArgumentException e) {
            // Ignore if not supported.
        }

        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception e) {
        throw MiscUtil.toUnchecked(e);
    }
}

From source file:org.cauldron.einstein.ri.core.model.data.xml.dom.DOMUtil.java

@Post
@Pre/*from   w  ww.  j ava  2s.  c  om*/
public static DocumentFragment buildFragmentFromNodeList(DocumentBuilder docBuilder, NodeList nodeList)
        throws IOException, TransformerException, SAXException {
    Document doc;
    DocumentFragment fragment;
    /*
    I know this is convoluted but it's very difficult to actually add a bunch of random nodes to
    a document fragment without getting errors. This works by normalizing the nodes.
     */
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byteArrayOutputStream.write("<dummy>".getBytes());
    Transformer xformer = transformerFactory.newTransformer();
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Source source = new DOMSource(nodeList.item(i));
        Result result = new StreamResult(byteArrayOutputStream);
        xformer.transform(source, result);
    }
    byteArrayOutputStream.write("</dummy>".getBytes());
    log.debug("Dumy node {0}.", byteArrayOutputStream);

    doc = docBuilder.parse(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
    fragment = doc.createDocumentFragment();
    nodeList = doc.getDocumentElement().getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        log.debug("Moving temporary node.");
        final Node node = nodeList.item(i);
        doc.adoptNode(node);
        fragment.appendChild(node);
    }
    log.debug("Fragment is now {0}.", fragment);
    return fragment;
}