0

我有一个 XML 文件,我正在尝试读取并转换为对象。我想转换并将所有位置放入一个填充有 Location 对象的数组中,这些对象由电影 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 次。否则,物体的形状很好,所以我想我做对了那部分。

4

2 回答 2

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引用,它应该可以解决问题。

于 2013-01-23T01:23:36.603 回答
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 输入是否正确。

于 2013-01-23T00:50:12.630 回答