Example usage for javax.xml.stream XMLEventWriter setPrefix

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

Introduction

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

Prototype

public void setPrefix(String prefix, String uri) throws XMLStreamException;

Source Link

Document

Sets the prefix the uri is bound to.

Usage

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

/**
 * Inits the namespace context of the XMLEventWriter:
 * <ul>/*from w w  w  .ja  va  2s.com*/
 * <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());
            }
        }
    }
}