使用XML中的对象填充数组

时间:2013-01-23 00:15:33

标签: java

我有一个XML文件,我正在尝试阅读并转换为对象。我想转换并将所有位置放在一个数组中,该数组中填充了由电影ID,日期和金额定义的位置对象。

这是我的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次。否则这些物体形成得很好,所以我想我得到了那个部分。

2 个答案:

答案 0 :(得分: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对其类字段的引用,这意味着更改该字段的值将为该类的所有实例更改它。

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

答案 1 :(得分: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输入是否正确。