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.jboss.windup.decorator.xml.XPathSummaryDecorator.java

protected String convertNode(Node node) throws TransformerException {
    StringWriter sw = new StringWriter();
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    sw.append(node.getTextContent());//from w ww  .  java  2s.c  om
    return sw.toString();
}

From source file:ac.uk.diamond.sample.HttpClientTest.Utils.java

static String xmlToString(Element doc) {
    StringWriter sw = new StringWriter();
    try {//from  w  w  w  . j a  v a  2s .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");

        transformer.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
    } catch (TransformerException e) {
        LOG.error("Unable to print message contents: ", e);
        return "<ERROR: " + e.getMessage() + ">";
    }
}

From source file:org.getwheat.harvest.library.dom.DomHelper.java

/**
 * Converts the {@link Document} to a string.
 * //w  w w .j  a v  a  2  s. c  om
 * @param document
 * @return XML string
 * @throws TransformerException
 */
public String convertToString(final Document document) throws TransformerException {
    final TransformerFactory factory = TransformerFactory.newInstance();
    final Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    final StringWriter writer = new StringWriter();
    final StreamResult result = new StreamResult(writer);
    final DOMSource source = new DOMSource(document);
    transformer.transform(source, result);
    return writer.toString();
}

From source file:com.c4om.utils.xmlutils.JavaXMLUtils.java

/**
 * Method that prints a W3C {@link Document} object to a provided {@link OutputStream}. 
 * Encoding should be correctly specified.
 * @param document the document to convert
 * @param os the output stream to print to
 * @param indent whether lines must be indented or not
 * @param omitDeclaration whether the XML declaration should be omitted or not. 
 * @param encoding the encoding placed at the XML declaration and used to encode the file.
 * @return the {@link String} representation of the document
 * @throws TransformerException if there are problems during the conversion
 *///from w w w . j a v  a  2s .  co m
public static void printW3CDocumentToOutputStream(Document document, OutputStream os, boolean indent,
        boolean omitDeclaration, String encoding) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration ? "yes" : "no");
    transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
    XmlStreamWriter writer;
    if (encoding != null) {
        transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        writer = new XmlStreamWriter(os, encoding);
    } else {
        writer = new XmlStreamWriter(os);
    }
    StreamResult streamResult = new StreamResult(writer);
    transformer.transform(new DOMSource(document), streamResult);
}

From source file:com.seajas.search.contender.service.modifier.ModifierScriptProcessor.java

/**
 * Transforms the given Reader content to an XSL-transformed document.
 *
 * @param script/*from w  w  w  .jav a 2 s  . c o m*/
 * @param input
 * @param uri
 * @return String
 * @throws ScriptException
 */
private String transformXML(final ModifierScript script, final String input, final URI uri)
        throws ScriptException {
    try {
        Transformer transformer = transformerCache.getTransformer(script.getId(), "modifier",
                script.getModificationDate());

        if (transformer == null)
            transformer = transformerCache.putContent(script.getId(), "modifier", script.getModificationDate(),
                    script.getScriptContent());

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

        Node document = documentBuilder.parse(new InputSource(new StringReader(input)));

        StringWriter buffer = new StringWriter();

        transformer.transform(new DOMSource(document), new StreamResult(buffer));

        return buffer.toString();
    } catch (TransformerConfigurationException e) {
        logger.error("Unable to generate a (cached) transformer from the given content", e);

        throw new ScriptException("Unable to generate a (cached) transformer from the given content");
    } catch (Exception e) {
        logger.error("Unable to perform content transformation modifier for " + uri, e);

        throw new ScriptException("Unable to perform content transformation modifier for " + uri);
    }
}

From source file:com.wavemaker.tools.pws.install.PwsInstall.java

public static void insertEntryKey(File xmlFile, File[] runtimeJarFiles, File[] toolsJarFiles,
        String partnerName) throws Exception {
    String content = getTrimmedXML(xmlFile);

    String fromStr1 = "<!DOCTYPE";
    String toStr1 = "<!--!DOCTYPE";
    content = content.replace(fromStr1, toStr1);

    String fromStr2 = "spring-beans-2.0.dtd\">";
    String toStr2 = "spring-beans-2.0.dtd\"-->";
    content = content.replace(fromStr2, toStr2);

    FileUtils.writeStringToFile(xmlFile, content, "UTF-8");

    InputStream is = new FileInputStream(xmlFile);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from  w  w  w.jav a2s . c o m
    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    Document doc = docBuilder.parse(is);

    insertEntryKey(doc, runtimeJarFiles, toolsJarFiles, partnerName);

    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    t.transform(new DOMSource(doc), new StreamResult(xmlFile));

    content = FileUtils.readFileToString(xmlFile, "UTF-8");
    content = content.replace(toStr1, fromStr1);

    content = content.replace(toStr2, fromStr2);

    FileUtils.writeStringToFile(xmlFile, content, "UTF-8");
}

From source file:org.jasig.portlet.campuslife.dao.ScreenScrapingService.java

/**
 * Get portlet-specific XML for clean, valid HTML.
 * //from  w  ww. j  a v  a2  s.  co  m
 * @param cleanHtml
 * @return
 * @throws TransformerException
 * @throws IOException
 */
protected String getXml(String cleanHtml) throws TransformerException, IOException {
    final StreamSource xsltSource = new StreamSource(xslt.getInputStream());
    final Transformer identityTransformer = transformerFactory.newTransformer(xsltSource);
    identityTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    final StringWriter outputWriter = new StringWriter();
    final StreamResult outputTarget = new StreamResult(outputWriter);
    final StreamSource xmlSource = new StreamSource(new StringReader(cleanHtml));
    identityTransformer.transform(xmlSource, outputTarget);
    final String content = outputWriter.toString();
    return content;
}

From source file:com.omertron.traileraddictapi.tools.DOMHelper.java

/**
 * Write the Document out to a file using nice formatting
 *
 * @param doc The document to save//  w  w  w  . j a v  a2 s .  c  o m
 * @param localFile The file to write to
 * @return
 */
public static boolean writeDocumentToFile(Document doc, String localFile) {
    try {
        TransformerFactory transfact = TransformerFactory.newInstance();
        Transformer trans = transfact.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, YES);
        trans.setOutputProperty(OutputKeys.INDENT, YES);
        trans.transform(new DOMSource(doc), new StreamResult(new File(localFile)));
        return true;
    } catch (TransformerConfigurationException ex) {
        LOG.warn(ERROR_WRITING_DOC, localFile, ex);
        return false;
    } catch (TransformerException ex) {
        LOG.warn(ERROR_WRITING_DOC, localFile, ex);
        return false;
    }
}

From source file:de.decidr.model.XmlToolsTest.java

@Test
public void testGetXmlStringDocumentProperties() {
    Properties properties = new Properties();
    properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    String result = XmlTools.getXmlString(SAMPLE_DOC, properties);
    assertEquals(SAMPLE_XML, result);/*from ww  w .  j  a  v  a2s .  c  om*/
}

From source file:curam.molsa.test.customfunctions.MOLSADOMReader.java

/**
 * This method is to transform the XML node to a string.
 * /*  w w  w  . j  a v  a2s  . com*/
 * @param node
 * @return buffer
 */
public static String toString(final Node node) {

    try {
        final TransformerFactory transFactory = TransformerFactory.newInstance();
        final Transformer transformer = transFactory.newTransformer();
        final StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(new DOMSource(node), new StreamResult(buffer));
        return buffer.toString();
    } catch (final TransformerException e) {
        e.printStackTrace();
    }
    return "";
}