2016-03-05 171 views
0

我想用Java讀取XML文件。這是我的XML文件,用Java讀取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 

我不明白爲什麼第二個值是相同的與第一。總座位價值是不同的,但其他人不是。我該怎麼辦?

回答

1

我確實更正了你的代碼,但我覺得這是一個更清潔的代碼版本。運行這段代碼,讓我知道,如果你面臨的問題....

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

}

+0

感謝。它的工作原理! – MrBoolean

+0

:)很高興它做到了... – Vineeth