Java XML Attribute Merge mergeByAttrib(ArrayList master, ArrayList slave, String attrib)

Here you can find the source of mergeByAttrib(ArrayList master, ArrayList slave, String attrib)

Description

merge By Attrib

License

Open Source License

Declaration

public static ArrayList<Element> mergeByAttrib(ArrayList<Element> master, ArrayList<Element> slave,
        String attrib) throws Exception 

Method Source Code

//package com.java2s;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import java.util.Map;

import org.w3c.dom.Element;

public class Main {
    /**/*from w  w  w .  j a  v  a 2 s. c om*/
     */
    public static ArrayList<Element> mergeByAttrib(ArrayList<Element> master, ArrayList<Element> slave,
            String attrib) throws Exception {
        Map<String, Element> masterMap = mapFromArrayListByAttribute(master, attrib);
        Map<String, Element> slaveMap = mapFromArrayListByAttribute(slave, attrib);
        slaveMap.putAll(masterMap);

        ArrayList<Element> res = new ArrayList<Element>();
        Collection<Element> collection = slaveMap.values();
        for (Element e : collection) {
            res.add(e);
        }
        return res;
    }

    public static Map<String, Element> mapFromArrayListByAttribute(ArrayList<Element> list, String attribName) {
        Map<String, Element> map = new HashMap<String, Element>();
        for (Element e : list) {
            map.put(e.getAttribute(attribName), e);
        }
        return map;
    }
}

Related

  1. mergeAttributes(StartElement tag, Iterator attrs, XMLEventFactory factory)
  2. mergeAttributes(StartElement tag, Iterator attrs, Iterator nsps, XMLEventFactory factory)