我有一個XML文件,我試圖讀取並轉換爲對象。我想要轉換並將所有位置填充到位置對象,這些位置對象由電影ID,日期和金額定義。用XML中的對象填充數組
這裏是我的XML文件:
這裏是我的代碼掃描位置XML部分:
public void findLocations() throws ParseException {
NodeList nList = document.getElementsByTagName("location");
Location[] locations = new Location[nList.getLength()];
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
locations[temp] = new Location(getTagValue("filmid", eElement), dateChanger(getTagValue("date", eElement)), getTagValue("amount", eElement));
System.out.println(locations[temp].getAmount()); //Outputs the good values.
}
}
System.out.println(locations[0].getAmount()); //Output : 5$
System.out.println(locations[1].getAmount()); //Output : 5$
System.out.println(locations[2].getAmount()); //Output : 5$
}
private static String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
這個問題似乎是我的陣列是越來越有相同的位置充滿3次,結束了用最後的位置填充3次。其他的東西都是很好的形成的,所以我想我得到了正確的部分。
你有一個'getTagValue'中的錯誤。使用調試器。 – Isaac
你得到了什麼TagValue方法呢。在這裏輸入代碼 – Jayamohan
我在原文中添加了它。 – metraon