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

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

Description

Like javanet.staxutils.XMLStreamUtils#mergeAttributes but it can also merge namespaces

License

Apache License

Declaration

public static StartElement mergeAttributes(StartElement tag, Iterator<? extends Attribute> attrs,
        Iterator<? extends Namespace> nsps, XMLEventFactory factory) 

Method Source Code

//package com.java2s;
/*//from  w w  w .  j  a v  a  2  s .c  o  m
   Copyright 2013, 2016 Nationale-Nederlanden
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

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.Namespace;
import javax.xml.stream.events.StartElement;

public class Main {
    /**
     * Like {@link javanet.staxutils.XMLStreamUtils#mergeAttributes} but it can
     * also merge namespaces
     */
    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());
    }
}

Related

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