繁体   English   中英

读取Java中的XML文件

[英]Read XML file in Java

我想通过Java读取XML文件。 这是我的XML文件,

<?xml version="1.0" encoding="UTF-8"?>
<votes>
  <city id="City A">
      <total_seats>8</total_seats>
      <party_votes>
        <party id ="Party A">
            <total_votes>100000</total_votes>
        </party>
        <party id ="Party B">
            <total_votes>80000</total_votes>
        </party>
        <party id ="Party C">
            <total_votes>30000</total_votes>
        </party>
        <party id ="Party D">
            <total_votes>20000</total_votes>
        </party>
      </party_votes>
   </city>
   <city id="City B">
      <total_seats>6</total_seats>
      <party_votes>
        <party id ="Party A">
            <total_votes>10000</total_votes>
        </party>
        <party id ="Party B">
            <total_votes>50000</total_votes>
        </party>
        <party id ="Party C">
            <total_votes>40000</total_votes>
        </party>
        <party id ="Party D">
            <total_votes>30000</total_votes>
        </party>
      </party_votes>
   </city>
</votes>

还有Java代码

File xmlFile = new File("C:\\Users\\..\\Desktop\\votes.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFile);

    doc.getDocumentElement().normalize();

    NodeList cityList = doc.getElementsByTagName("city");
        NodeList partyList = doc.getElementsByTagName("party");//output 8

    for (int temp = 0; temp < cityList.getLength(); temp++) {

        Node cityNode = cityList.item(temp);

        if (cityNode.getNodeType() == Node.ELEMENT_NODE) {

            Element cityElement = (Element) cityNode;

            System.out.println("City  : " + cityElement.getAttribute("id"));
            System.out.println("Total Seats : " + cityElement.getElementsByTagName("total_seats").item(0).getTextContent());

                        for (int index = 0; index < partyList.getLength()/2; index++) {

                            Node partyNode = partyList.item(index);

                            if(partyNode.getNodeType() == Node.ELEMENT_NODE) {

                                Element partyElement = (Element) partyNode;

                                System.out.println("Party : " + partyElement.getAttribute("id"));
                                System.out.println("Total Votes : " + partyElement.getElementsByTagName("total_votes").item(0).getTextContent());

                            }
                        }
                        System.out.println("");
        }
    }

这是输出,

City  : City A
Total Seats : 8
Party : Party A
Total Votes : 100000
Party : Party B
Total Votes : 80000
Party : Party C
Total Votes : 30000
Party : Party D
Total Votes : 20000

City  : City B
Total Seats : 6
Party : Party A
Total Votes : 100000
Party : Party B
Total Votes : 80000
Party : Party C
Total Votes : 30000
Party : Party D
Total Votes : 20000

我不明白为什么第二个值与第一个相同。 总席位值有所不同,其他则没有。 我该怎么办?

我确实纠正了您的代码,但是我觉得这是一个更干净的代码版本。 运行此代码,让我知道您是否遇到问题...。

    public static void main(String arg[]) throws ParserConfigurationException,
        SAXException, IOException {
    File xmlFile = new File("votes.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFile);

    doc.getDocumentElement().normalize();

    NodeList serviceNodeList = doc.getElementsByTagName("city"); 

    for (int servicenodeCount = 0; servicenodeCount < serviceNodeList.getLength(); servicenodeCount++) {
        Node servicenode = serviceNodeList.item(servicenodeCount);
        if (servicenode.getNodeType() == Node.ELEMENT_NODE) {
            Element singleServiceNode = (Element) servicenode;
            NodeList functionNodeList = singleServiceNode.getElementsByTagName("party_votes");

            System.out.println("City  : " + singleServiceNode.getAttribute("id"));

            for(int functionNodeCount = 0; functionNodeCount < functionNodeList.getLength();functionNodeCount++){
                 Node functionNode = functionNodeList.item(functionNodeCount);                      
                 if (functionNode.getNodeType() == Node.ELEMENT_NODE) {
                     Element singleFunctionNode = (Element) servicenode;
                     NodeList mappingNodeList = singleFunctionNode.getElementsByTagName("party");

                     System.out.println("Total Seats : " + singleFunctionNode.getElementsByTagName("total_seats").item(0).getTextContent());

                     for(int genericNodeCount = 0; genericNodeCount < mappingNodeList.getLength();genericNodeCount++){
                         Node genericNode = mappingNodeList.item(genericNodeCount);
                         if (genericNode.getNodeType() == Node.ELEMENT_NODE) {
                             Element singleGenericElement = (Element) genericNode;
                            System.out.println("Party : " + singleGenericElement.getAttribute("id"));
                            System.out.println("First Name : " + singleGenericElement.getElementsByTagName("total_votes").item(0).getTextContent());
                         }
                     }
                 }
            }
        }
    }
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM