読み取ってオブジェクトに変換しようとしている XML ファイルがあります。映画ID、日付、金額で定義されたLocationオブジェクトで満たされた配列にすべての場所を変換して入れたいと思います。
ここに私の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 回いっぱいになるようです。それ以外のオブジェクトはよく形成されているので、その部分は正しいと思います。