Java tutorial
import java.io.File; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static void main(String[] args) throws Exception { List<Car> carsList = new ArrayList<Car>(); Set<Car> carsset = new HashSet<Car>(); File fXmlFile = new File("cars.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("cars"); Car tempCar = null; for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { tempCar = new Car(); Element eElement = (Element) nNode; tempCar.setModel(eElement.getElementsByTagName("model").item(0).getTextContent()); tempCar.setVersion(eElement.getElementsByTagName("version").item(0).getTextContent()); carsList.add(tempCar); carsset.add(tempCar); } } for (Car cs : carsset) { int count = 0; for (Car cl : carsList) { if (cs.equals(cl)) { count = count + 1; } } System.out.println(cs + "\t" + count); } } } class Car { private String model; private String version; public String getModel() { return model; } public void setModel(String model) { this.model = model; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } @Override public boolean equals(Object obj) { if (this.model.equals(((Car) obj).getModel()) && this.version.equals(((Car) obj).getVersion())) { return true; } return false; } @Override public int hashCode() { return this.model.length() + this.version.length(); } @Override public String toString() { return this.model + "\t " + this.version; } }