Nice question…This problem taken me back to basics of java.. Here we go for the solution..
Car.java
package com.sof.test;
public 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;
}
}
cars.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<cars>
<model>maruthi</model>
<version>alto</version>
</cars>
<cars>
<model>maruthi</model>
<version>verna</version>
</cars>
<cars>
<model>maruthi</model>
<version>verna</version>
</cars>
<cars>
<model>maruthi</model>
<version>alto</version>
</cars>
<cars>
<model>maruthi</model>
<version>alto</version>
</cars>
<cars>
<model>honda</model>
<version>city</version>
</cars>
<cars>
<model>Audi</model>
<version>Q7</version>
</cars>
<cars>
<model>honda</model>
<version>city</version>
</cars>
</root>
Tester.java
package com.sof.test;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
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 Tester {
public static void main(String[] args) {
List<Car> carsList=new ArrayList<Car>();
Set<Car> carsset=new HashSet<Car>();
try {
File fXmlFile = new File(
"D:\\StackOverFlow\\DomParserTest\\src\\com\\sof\\test\\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);
}
}
// System.out.println(carsset.size()+"\t"+carsList.size());
System.out.println("**********************************");
for(Car cs:carsset)
{
int count=0;
for(Car cl:carsList)
{
if(cs.equals(cl))
{
count=count+1;
}
}
System.out.println(cs+"\t"+count);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
output will be:
**********************************
Audi Q7 1
honda city 2
maruthi alto 3
maruthi verna 2
let me know if you face any issues….
If you get this accept it as answer 🙂
9
solved To get count of values with respective attribute values in XML using java [closed]