Java tutorial
//package com.java2s; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.xml.namespace.QName; import javax.xml.stream.XMLEventFactory; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.StartElement; public class Main { /** * 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. * * @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()); } }