Example usage for javax.xml.stream XMLEventWriter setDefaultNamespace

List of usage examples for javax.xml.stream XMLEventWriter setDefaultNamespace

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventWriter setDefaultNamespace.

Prototype

public void setDefaultNamespace(String uri) throws XMLStreamException;

Source Link

Document

Binds a URI to the default namespace This URI is bound in the scope of the current START_ELEMENT / END_ELEMENT pair.

Usage

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

/**
 * Inits the namespace context of the XMLEventWriter:
 * <ul>/*from  w w  w.  jav a  2s  .c o  m*/
 * <li>rootTagNamespacePrefix for rootTagName</li>
 * <li>any other xmlns namespace prefix declarations in the root element attributes</li>
 * </ul>
 * 
 * @param writer XML event writer
 * @throws XMLStreamException
 */
protected void initNamespaceContext(XMLEventWriter writer) throws XMLStreamException {
    if (StringUtils.hasText(getRootTagNamespace())) {
        if (StringUtils.hasText(getRootTagNamespacePrefix())) {
            writer.setPrefix(getRootTagNamespacePrefix(), getRootTagNamespace());
        } else {
            writer.setDefaultNamespace(getRootTagNamespace());
        }
    }
    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);
                }
                if (log.isDebugEnabled()) {
                    log.debug("registering prefix: " + prefix + "=" + entry.getValue());
                }
                writer.setPrefix(prefix, entry.getValue());
            }
        }
    }
}