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.gluu.saml.Response.java

public void printDocument(OutputStream out) 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.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

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

From source file:org.gnucash.android.export.ExporterTask.java

/**
 * Writes out the document held in <code>node</code> to <code>outputWriter</code>
 * @param node {@link Node} containing the OFX document structure. Usually the parent node
 * @param outputWriter {@link Writer} to use in writing the file to stream
 * @param omitXmlDeclaration Flag which causes the XML declaration to be omitted
 *//* w  w w.j a v  a 2  s . c om*/
public void write(Node node, Writer outputWriter, boolean omitXmlDeclaration) {
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(node);
        StreamResult result = new StreamResult(outputWriter);

        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        if (omitXmlDeclaration) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }

        transformer.transform(source, result);
    } catch (TransformerConfigurationException txconfigException) {
        txconfigException.printStackTrace();
    } catch (TransformerException tfException) {
        tfException.printStackTrace();
    }
}

From source file:org.infoscoop.dao.model.TabLayout.java

private String getNodeListString(NodeList list)
        throws TransformerFactoryConfigurationError, TransformerException {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < list.getLength(); i++) {
        Node widget = list.item(i);
        StringWriter buf = new StringWriter();
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        tf.transform(new DOMSource(widget), new StreamResult(buf));

        sb.append(buf + "\n");
    }//ww  w.  j av a2 s.co  m

    return sb.toString();
}

From source file:org.infoscoop.request.filter.HTMLFragmentFilter.java

private String document2String(Document doc) {
    try {/*from   ww w . jav a2s.  c  o  m*/
        Source source = new DOMSource(doc);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (TransformerException ex) {
        throw new RuntimeException("unexcepted error.", ex);
    }
}

From source file:org.jasig.resourceserver.utils.aggr.ResourcesElementsProviderImpl.java

@Override
public String getResourcesHtmlFragment(HttpServletRequest request, String skinXml) {
    final Included includedType = this.getIncludedType(request);

    String htmlFragment;/*from  ww w .  j a  v a  2 s.c o m*/
    if (Included.AGGREGATED == includedType) {
        htmlFragment = this.htmlResourcesCache.get(skinXml);
        if (htmlFragment != null) {
            return htmlFragment;
        }
    }

    final DocumentFragment resourcesXml = this.getResourcesXml(request, skinXml);

    final StringWriter stringWriter = new StringWriter();
    try {
        final Transformer transformer = transformerFactory.newTransformer();
        if (Included.PLAIN == this.getIncludedType(request)) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        }
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(resourcesXml), new StreamResult(stringWriter));
    } catch (Exception e) {
        throw new RuntimeException("Failed to serialize XML fragment to HTML fragment", e);
    }

    htmlFragment = stringWriter.toString();
    if (Included.AGGREGATED == includedType) {
        this.htmlResourcesCache.put(skinXml, htmlFragment);
    }
    return htmlFragment;
}

From source file:org.jboss.bpm.console.server.util.DOMUtils.java

public static Element sourceToElement(Source source) throws IOException {
    Element retElement = null;//w ww.  ja va2s .co  m

    try {
        if (source instanceof StreamSource) {
            StreamSource streamSource = (StreamSource) source;

            InputStream ins = streamSource.getInputStream();
            if (ins != null) {
                retElement = DOMUtils.parse(ins);
            } else {
                Reader reader = streamSource.getReader();
                retElement = DOMUtils.parse(new InputSource(reader));
            }
        } else if (source instanceof DOMSource) {
            DOMSource domSource = (DOMSource) source;
            Node node = domSource.getNode();
            if (node instanceof Element) {
                retElement = (Element) node;
            } else if (node instanceof Document) {
                retElement = ((Document) node).getDocumentElement();
            } else {
                throw new RuntimeException("Unsupported Node type: " + node.getClass().getName());
            }
        } else if (source instanceof SAXSource) {
            // The fact that JAXBSource derives from SAXSource is an implementation detail.
            // Thus in general applications are strongly discouraged from accessing methods defined on SAXSource.
            // The XMLReader object obtained by the getXMLReader method shall be used only for parsing the InputSource object returned by the getInputSource method.

            TransformerFactory tf = TransformerFactory.newInstance();
            ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.transform(source, new StreamResult(baos));
            retElement = DOMUtils.parse(new ByteArrayInputStream(baos.toByteArray()));
        } else {
            throw new RuntimeException("Source type not implemented: " + source.getClass().getName());
        }

    } catch (TransformerException ex) {
        IOException ioex = new IOException();
        ioex.initCause(ex);
        throw ioex;
    }

    return retElement;
}

From source file:org.kapott.hbci.tools.TransactionsToXML.java

public void writeXMLString(Document doc, OutputStream out) {
    if (doc == null) {
        throw new NullPointerException("document must not be null");
    }/*from www.  ja  v  a 2 s  .  c  o  m*/
    if (out == null) {
        throw new NullPointerException("output stream must not be null");
    }
    try {
        TransformerFactory transFac = TransformerFactory.newInstance();
        Transformer trans = transFac.newTransformer();

        trans.setOutputProperty(OutputKeys.METHOD, "xml");
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        Source source = new DOMSource(doc);
        Result target = new StreamResult(out);
        trans.transform(source, target);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.kepler.kar.karxml.KarXml.java

public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {/*from  w w  w .  j av  a  2 s  .c om*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        NodeList nodes = node.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            t.transform(new DOMSource(nodes.item(i)), new StreamResult(sw));
        }
    } catch (TransformerException ex) {
        System.out.println("Transformer exception");
    }
    return sw.toString();
}

From source file:org.kuali.kfs.module.tem.batch.service.impl.TemProfileExportServiceImpl.java

protected String generateXMLDoc(List<TemProfile> profiles)
        throws ParserConfigurationException, TransformerException {

    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
    Document doc = docBuilder.newDocument();

    //Create the Profile Header
    Element profileHeader = doc.createElement("profileHeader");
    profileHeader.appendChild(createElement(doc, "dateOfExport",
            dateTimeService.toDateTimeString(dateTimeService.getCurrentDate())));
    profileHeader.appendChild(createElement(doc, "universityName", parameterService.getParameterValueAsString(
            KfsParameterConstants.FINANCIAL_SYSTEM_ALL.class, KfsParameterConstants.INSTITUTION_NAME)));

    //Create the Profile Detail section
    Element profileDetailList = doc.createElement("profiles");
    for (TemProfile profile : profiles) {
        profileDetailList.appendChild(generateProfileDetailElement(doc, profile));
    }//from  ww w .  j a  v  a 2  s  .co  m

    //Create the Root Element, add the Profile Header and Detail sections, add to document
    Element rootElement = doc.createElement("temProfileExport");
    rootElement.appendChild(profileHeader);
    rootElement.appendChild(profileDetailList);
    doc.appendChild(rootElement);

    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");

    //create string from xml tree
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(doc);
    trans.transform(source, result);

    return sw.toString();
}

From source file:org.kuali.kfs.module.tem.batch.TemProfileExportStep.java

private String generateXMLDoc(List<TemProfile> profiles)
        throws ParserConfigurationException, TransformerException {

    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
    Document doc = docBuilder.newDocument();

    //Create the Profile Header
    Element profileHeader = doc.createElement("profileHeader");
    profileHeader.appendChild(createElement(doc, "dateOfExport",
            getDateTimeService().toDateTimeString(getDateTimeService().getCurrentDate())));
    profileHeader/*w  w w .ja  v  a2 s  .  c om*/
            .appendChild(createElement(doc, "universityName", getParameterService().getParameterValueAsString(
                    KfsParameterConstants.FINANCIAL_SYSTEM_ALL.class, KfsParameterConstants.INSTITUTION_NAME)));

    //Create the Profile Detail section
    Element profileDetailList = doc.createElement("profiles");
    for (TemProfile profile : profiles) {
        profileDetailList.appendChild(generateProfileDetailElement(doc, profile));
    }

    //Create the Root Element, add the Profile Header and Detail sections, add to document
    Element rootElement = doc.createElement("temProfileExport");
    rootElement.appendChild(profileHeader);
    rootElement.appendChild(profileDetailList);
    doc.appendChild(rootElement);

    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");

    //create string from xml tree
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(doc);
    trans.transform(source, result);

    return sw.toString();
}