使用java获取XML中具有相应属性值的值的计数

时间:2014-02-06 17:40:43

标签: java xml

我有一个xml文件,如:

<cars>
    <model>maruthi</model>
    <version>alto</version>
</cars>
<cars>
    <model>maruthi</model>
    <version>verna</version>
</cars>
<cars>
    <model>maruthi</model>
    <version>alto</version>
</cars>

我需要java程序,它可以读取文件并提供输出,如

            model/ccar      Alto  verna
            Maruthi           2      1

(就像该模型的模型和计数版本一样......)

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

好问题......这个问题让我回到了java的基础知识..我们在这里寻求解决方案..

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();
        }
    }

}

输出将是:

**********************************
Audi     Q7    1
honda     city    2
maruthi     alto    3
maruthi     verna    2

如果您遇到任何问题,请告诉我.... 如果你得到这个接受它作为答案:)