Java XML Attribute Merge mergeAttributes(StartElement tag, Iterator attrs, XMLEventFactory factory)

Here you can find the source of mergeAttributes(StartElement tag, Iterator attrs, XMLEventFactory factory)

Description

Constructs a new StartElement that merges the attributes and namespaces found in the specified StartElement, with the provided attributes.

License

Open Source License

Parameter

Parameter Description
tag The original StartElement
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.

Declaration

public static StartElement mergeAttributes(StartElement tag, Iterator attrs, XMLEventFactory factory) 

Method Source Code


//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 {
    /**/*from w w w  .  j a  v a  2  s.  c o  m*/
     * 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());

    }
}

Related

  1. mergeAttributes(StartElement tag, Iterator attrs, Iterator nsps, XMLEventFactory factory)
  2. mergeByAttrib(ArrayList master, ArrayList slave, String attrib)