java - 用Java读取XML文件

标签 java xml

我想用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

我不明白为什么第二个值与第一个值相同。总座位值(value)不同,但其他则不同。我该怎么办?

最佳答案

我确实纠正了你的代码,但我觉得,这是一个更干净的代码版本。运行此代码,如果您遇到问题,请告诉我......

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

}

关于java - 用Java读取XML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35818048/

相关文章:

java - 如何取回 JTable 的数据?

java - 如何使用 Android 的 Handler.PostDelayed 使事件在指定时间发生?

java - Android XML, Activity 编辑文本

xml - perl解析xml文件后,得到特殊字符

java - 如何在android中的字符串资源中使用整数资源

java - 为什么从 XAResource 抛出 XAException 仍然不允许事务提交成功?

java - 如何在处理 Java Streams 时从 Java IDE 请求更多返回类型信息?

java - 是否可以声明一个全局变量,以便可以在 JSP 中的每个类中使用它?

javascript - xsl 添加空白空间?

java - 使用 MOXy 时使用 IDRef 编码 XML 标签的问题