我有一个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