Example usage for javax.xml.stream XMLEventFactory createIgnorableSpace

List of usage examples for javax.xml.stream XMLEventFactory createIgnorableSpace

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventFactory createIgnorableSpace.

Prototype

public abstract Characters createIgnorableSpace(String content);

Source Link

Document

Create an ignorable space

Usage

From source file:org.emonocot.job.io.StaxEventItemWriter.java

/**
 * Writes simple XML header containing:/*from w w  w.  j a  v  a2s .  c om*/
 * <ul>
 * <li>xml declaration - defines encoding and XML version</li>
 * <li>opening tag of the root element and its attributes</li>
 * </ul>
 * If this is not sufficient for you, simply override this method. Encoding,
 * version and root tag name can be retrieved with corresponding getters.
 *
 * @param writer
 *            XML event writer
 * @throws XMLStreamException if there is a problem starting the document
 */
protected final void startDocument(final XMLEventWriter writer) throws XMLStreamException {

    XMLEventFactory factory = XMLEventFactory.newInstance();

    // write start document
    writer.add(factory.createStartDocument(getEncoding(), getVersion()));

    // write root tag
    writer.add(
            factory.createStartElement(getRootTagNamespacePrefix(), getRootTagNamespace(), getRootTagName()));
    if (StringUtils.hasText(getRootTagNamespace())) {
        if (StringUtils.hasText(getRootTagNamespacePrefix())) {
            writer.add(factory.createNamespace(getRootTagNamespacePrefix(), getRootTagNamespace()));
        } else {
            writer.add(factory.createNamespace(getRootTagNamespace()));
        }
    }

    // write root tag attributes
    if (!CollectionUtils.isEmpty(getRootElementAttributes())) {

        for (Map.Entry<String, String> entry : getRootElementAttributes().entrySet()) {
            String key = entry.getKey();
            if (key.startsWith("xmlns")) {
                String prefix = "";
                if (key.contains(":")) {
                    prefix = key.substring(key.indexOf(":") + 1);
                }
                writer.add(factory.createNamespace(prefix, entry.getValue()));
            } else {
                writer.add(factory.createAttribute(key, entry.getValue()));
            }
        }

    }

    /*
     * This forces the flush to write the end of the root element and avoids
     * an off-by-one error on restart.
     */
    writer.add(factory.createIgnorableSpace(""));
    writer.flush();

}

From source file:org.springframework.batch.item.xml.StaxEventItemWriter.java

/**
 * Writes simple XML header containing://ww  w  .j a va  2 s.co  m
 * <ul>
 * <li>xml declaration - defines encoding and XML version</li>
 * <li>opening tag of the root element and its attributes</li>
 * </ul>
 * If this is not sufficient for you, simply override this method. Encoding,
 * version and root tag name can be retrieved with corresponding getters.
 * 
 * @param writer XML event writer
 * @throws XMLStreamException
 */
protected void startDocument(XMLEventWriter writer) throws XMLStreamException {

    XMLEventFactory factory = createXmlEventFactory();

    // write start document
    writer.add(factory.createStartDocument(getEncoding(), getVersion()));

    // write root tag
    writer.add(
            factory.createStartElement(getRootTagNamespacePrefix(), getRootTagNamespace(), getRootTagName()));
    if (StringUtils.hasText(getRootTagNamespace())) {
        if (StringUtils.hasText(getRootTagNamespacePrefix())) {
            writer.add(factory.createNamespace(getRootTagNamespacePrefix(), getRootTagNamespace()));
        } else {
            writer.add(factory.createNamespace(getRootTagNamespace()));
        }
    }

    // write root tag attributes
    if (!CollectionUtils.isEmpty(getRootElementAttributes())) {

        for (Map.Entry<String, String> entry : getRootElementAttributes().entrySet()) {
            String key = entry.getKey();
            if (key.startsWith("xmlns")) {
                String prefix = "";
                if (key.contains(":")) {
                    prefix = key.substring(key.indexOf(":") + 1);
                }
                writer.add(factory.createNamespace(prefix, entry.getValue()));
            } else {
                writer.add(factory.createAttribute(key, entry.getValue()));
            }
        }

    }

    /*
     * This forces the flush to write the end of the root element and avoids
     * an off-by-one error on restart.
     */
    writer.add(factory.createIgnorableSpace(""));
    writer.flush();

}