Java 用XML中的对象填充数组

Java 用XML中的对象填充数组,java,Java,我有一个XML文件,我正试图读取并转换为对象。我想转换并将所有位置放在一个数组中,数组中填充了位置对象,这些对象由胶片id、日期和数量定义 这是我的XML文件: 以下是我扫描位置XML部分的代码: public void findLocations() throws ParseException { NodeList nList = document.getElementsByTagName("location"); Location[] locations = new Lo

我有一个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();
}
public void findLocations()引发异常{
NodeList nList=document.getElementsByTagName(“位置”);
Location[]locations=新位置[nList.getLength()];
对于(int-temp=0;temp

问题似乎是我的数组在同一个位置被填充了3次,最后一个位置被填充了3次。其他方面的对象都是格式良好的,所以我想我把这部分做对了。

您的源代码工作得很好。只需修改源代码以输出标记,如下所示

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();
        }
    }
}

检查您的XML输入是否正确。

您可以改用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();
        }
    }
}
反馈后更新

位置
类维护对其类字段的
静态
引用,这意味着更改该字段的值将更改该类的所有实例的值


删除
静态
引用,应该可以解决问题。

您在
getTagValue
中有一个bug。使用调试器。getTagValue方法做什么。在这里输入代码我在原始帖子中添加了它。我得到了完美的输出,没有任何问题。如果我使用数组返回值,这就是我得到的。System.out.println(位置[0].getAmount())//输出:5美元不,仍然没有做我想要的。当我把所有的位置放在一个由位置对象组成的数组中时,它仍然是同一个胶片。位置[索引]=新回收(dateNode.getTextContent()、dateChanger2(filmIDNode.getTextContent()、amountNode.getTextContent());System.out.println(reclamations[i++].getFilmId())//在循环中输出良好的胶片Id//在循环外输出所有相同的胶片Id然后听起来好像您的位置对象有问题。您是否使用了
静态
变量?是的,它们是私有静态变量。这就是您的问题
static
是一个共享变量,这意味着如果在一个实例中更改它,那么所有实例都会更改它。噢!我现在明白了,我把它放在一个物体阵列中可以防止这种情况。非常感谢你!我必须将此帖子标记为解决方案吗?
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$