2013-01-23 87 views
0

我有一个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次。其他的东西都是很好的形成的,所以我想我得到了正确的部分。

+0

你有一个'getTagValue'中的错误。使用调试器。 – Isaac

+0

你得到了什么TagValue方法呢。在这里输入代码 – Jayamohan

+0

我在原文中添加了它。 – metraon

回答

1

您可以使用XPath,而不是...

public class TestXML03 { 

    public static void main(String[] args) { 

     try { 
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
      factory.setNamespaceAware(false); 
      DocumentBuilder builder = factory.newDocumentBuilder(); 
      Document xmlDoc = builder.parse(new File("Test.xml")); 

      Node root = xmlDoc.getDocumentElement(); 

      XPathFactory xFactory = XPathFactory.newInstance(); 
      XPath xPath = xFactory.newXPath(); 

      XPathExpression xExpress = xPath.compile("/file/location"); 
      NodeList nodes = (NodeList) xExpress.evaluate(root, XPathConstants.NODESET); 

      System.out.println("Found " + nodes.getLength() + " location nodes"); 
      System.out.println(""); 

      for (int index = 0; index < nodes.getLength(); index++) { 
       Node node = nodes.item(index); 
       xExpress = xPath.compile("filmid"); 
       Node filmIDNode = (Node) xExpress.evaluate(node, XPathConstants.NODE); 
       System.out.println(filmIDNode.getNodeName() + " = " + filmIDNode.getTextContent()); 

       xExpress = xPath.compile("date"); 
       Node dateNode = (Node) xExpress.evaluate(node, XPathConstants.NODE); 
       System.out.println(dateNode.getNodeName() + " = " + dateNode.getTextContent()); 

       xExpress = xPath.compile("amount"); 
       Node amountNode = (Node) xExpress.evaluate(node, XPathConstants.NODE); 
       System.out.println(amountNode.getNodeName() + " = " + amountNode.getTextContent()); 

       System.out.println(""); 
      } 

     } catch (Exception exp) { 
      exp.printStackTrace(); 
     } 
    } 
} 

,输出...

Found 3 location nodes 

filmid = 100 
date = 2013-01-11 
amount = 4.00$ 

filmid = 200 
date = 2013-01-13 
amount = 9.00$ 

filmid = 334 
date = 2013-01-23 
amount = 5.00$ 

更新反馈

Location类保持static引用之后到它的类字段,这意味着改变字段的值会改变它的所有t的实例帽类。

删除static引用,它应该解决问题。

+0

不要再做我想做的事了。当我将所有位置放置在由位置对象组成的数组中时,它仍然是同一部影片。 locations [index] = new Reclamation(dateNode.getTextContent(),dateChanger2(filmIDNode.getTextContent()),amountNode.getTextContent()); System.out.println(reclamations [i ++]。getFilmId()); //在循环中输出好的电影ID //在循环外输出所有相同的电影ID – metraon

+0

然后听起来好像您的位置对象有问题。你在使用'static'变量吗? – MadProgrammer

+0

是的,它们是私有静态变量。 – metraon

0

您的源代码正在运行完美。只需将您的源代码修改为如下输出标签。

public static void findLocations(Document document) 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; 
      System.out.println(getTagValue("filmid", eElement)); 
      System.out.println(getTagValue("date", eElement)); 
      System.out.println(getTagValue("amount", eElement)); 
      System.out.println(); 
     } 
    } 
} 

我得到了正确的输出

100 
2013-01-11 
4.00$ 

200 
2013-01-13 
9.00$ 

334 
2013-01-23 
5.00$ 

检查,如果你的XML输入正确。

+0

那就是我得到的,如果我使用该数组返回值。 System.out.println(locations [0] .getAmount()); //输出:5 $ – metraon