如果遇到某些条件,我如何通过节点列表进行迭代并替换内容?

问题描述 投票:1回答:1

我尝试迭代你可以看到下面的.xml文件并检查每个用户(这里:user1和user2),如果current-tag的值与total-tag的值相同,如果没有,则添加当前标记的值的某个值(例如+10)。

<config>
    <users>
        <user1>
            <name>test1</name>
            <stats>
                <total>50</total>
                <current>50</current>
            </stats>
        </user1>
        <user2>
            <name>test2</name>
            <stats>
                <total>50</total>
                <current>20</current>
            </stats>
        </user2>
    </users>
</config>

我使用以下代码迭代stats-tag的子节点,并为每个用户比较current-tag的值和total-tag的值:

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;
        docBuilder = docFactory.newDocumentBuilder();

        Document doc = docBuilder.parse(names_saves);
        doc.getDocumentElement().normalize();

        NodeList stats = doc.getElementsByTagName("stats");

        for(int i = 0; i < stats.getLength(); i++) {
            org.w3c.dom.Node n = stats.item(i);
            NodeList children= n.getChildNodes();
            for ( int x = 0; x < children.getLength(); x++ ) {
                Node elem = children.item(x);

                int current = 0;
                int total = 0;

                switch ( elem.getNodeName() ) {
                    case "current":
                        current = Integer.parseInt(elem.getTextContent());
                        break;
                    case "total":
                        total = Integer.parseInt(elem.getTextContent());
                        break;
                }

                if(current < total) {
                    Element element = (Element) elem;
                    element.getElementsByTagName("current").item(0).setTextContent(Integer.toString(current + 10));
                }
            }
        }

这段代码的问题是它在element.getElementsByTagName(...抛出一个NullPointerException。我已经打印出变量currenttotal的值,它们显示正确的值,但是当我在if(current < total)进行比较时,它总是正确的,而对于user1和user2都不是false。

最后,它应该是这样的:

<config>
    <users>
        <user1>
            <name>test1</name>
            <stats>
                <total>50</total>
                <current>50</current> <!-- Is already the same the value of total, don't edit -->
            </stats>
        </user1>
        <user2>
            <name>test2</name>
            <stats>
                <total>50</total>
                <current>30</current> <!-- Not the same, add 10 to former value -->
            </stats>
        </user2>
    </users>
</config>
java xml dom
1个回答
1
投票

问题是当前和总计的比较是在填充两个变量的循环内。所以只有在填充一个变量后才进行比较。你需要在循环外移动if,并更好地检查两个变量确实设置为大于零的值(假设这个值不能作为输入)

        int current = 0;
        int total = 0;
        for ( int x = 0; x < children.getLength(); x++ ) {
            Node elem = children.item(x);

            switch ( elem.getNodeName() ) {
                case "current":
                    current = Integer.parseInt(elem.getTextContent());
                    break;
                case "total":
                    total = Integer.parseInt(elem.getTextContent());
                    break;
            }

        }
        if(current > 0 && total > 0 && current < total) {
            Element element = (Element) elem;
            element.getElementsByTagName("current").item(0).setTextContent(Integer.toString(current + 10));
        }
© www.soinside.com 2019 - 2024. All rights reserved.