Example usage for javax.xml.stream XMLEventFactory createStartElement

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

Introduction

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

Prototype

public abstract StartElement createStartElement(String prefix, String namespaceUri, String localName,
        Iterator<? extends Attribute> attributes, Iterator<? extends Namespace> namespaces,
        NamespaceContext context);

Source Link

Document

Create a new StartElement.

Usage

From source file:Main.java

/**
 * Constructs a new StartElement that merges the attributes and namespaces
 * found in the specified StartElement, with the provided attributes. The
 * returned StartElement will contain all the attributes and namespaces of
 * the original, plus those defined in the map.
 * /*from   ww  w . j av  a  2  s  .  c  o  m*/
 * @param tag The original StartElement
 * @param attrs An iterator of Atributes to add to the element.
 * @return A new StartElement that contains all the original attributes and
 *         namespaces, plus the provided attributes.
 */
public static StartElement mergeAttributes(StartElement tag, Iterator attrs, XMLEventFactory factory) {

    // create Attribute map
    Map attributes = new HashMap();

    // iterate through start tag's attributes
    for (Iterator i = tag.getAttributes(); i.hasNext();) {

        Attribute attr = (Attribute) i.next();
        attributes.put(attr.getName(), attr);

    }

    // iterate through new attributes
    while (attrs.hasNext()) {

        Attribute attr = (Attribute) attrs.next();
        attributes.put(attr.getName(), attr);

    }

    factory.setLocation(tag.getLocation());

    QName tagName = tag.getName();
    return factory.createStartElement(tagName.getPrefix(), tagName.getNamespaceURI(), tagName.getLocalPart(),
            attributes.values().iterator(), tag.getNamespaces(), tag.getNamespaceContext());

}

From source file:Main.java

/**
 * Like {@link javanet.staxutils.XMLStreamUtils#mergeAttributes} but it can
 * also merge namespaces//w  ww  .j  av a 2  s . c  o m
 * 
 * @param tag
 * @param attrs
 * @param nsps
 */
public static StartElement mergeAttributes(StartElement tag, Iterator<? extends Attribute> attrs,
        Iterator<? extends Namespace> nsps, XMLEventFactory factory) {
    // create Attribute map
    Map<QName, Attribute> attributes = new HashMap<QName, Attribute>();

    // iterate through start tag's attributes
    for (Iterator i = tag.getAttributes(); i.hasNext();) {
        Attribute attr = (Attribute) i.next();
        attributes.put(attr.getName(), attr);
    }
    if (attrs != null) {
        // iterate through new attributes
        while (attrs.hasNext()) {
            Attribute attr = attrs.next();
            attributes.put(attr.getName(), attr);
        }
    }

    Map<QName, Namespace> namespaces = new HashMap<QName, Namespace>();
    for (Iterator i = tag.getNamespaces(); i.hasNext();) {
        Namespace ns = (Namespace) i.next();
        namespaces.put(ns.getName(), ns);
    }
    if (nsps != null) {
        while (nsps.hasNext()) {
            Namespace ns = nsps.next();
            namespaces.put(ns.getName(), ns);
        }
    }

    factory.setLocation(tag.getLocation());

    QName tagName = tag.getName();
    return factory.createStartElement(tagName.getPrefix(), tagName.getNamespaceURI(), tagName.getLocalPart(),
            attributes.values().iterator(), namespaces.values().iterator(), tag.getNamespaceContext());
}