我想通过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
我不明白为什么第二个值与第一个值相同。总座位数值不同,但其他人不同。我该怎么办?
答案 0 :(得分: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());
}
}
}
}
}
}
}
}