簡體   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