Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果满足某些条件,如何迭代节点列表并替换内容?_Java_Xml_Dom - Fatal编程技术网

Java 如果满足某些条件,如何迭代节点列表并替换内容?

Java 如果满足某些条件,如何迭代节点列表并替换内容?,java,xml,dom,Java,Xml,Dom,我尝试遍历下面可以看到的.xml文件,检查每个用户(这里是user1和user2),如果当前标记的值与total标记的值相同,如果不相同,则向当前标记的值添加某个值(例如+10) <config> <users> <user1> <name>test1</name> <stats> <total>50</

我尝试遍历下面可以看到的.xml文件,检查每个用户(这里是user1和user2),如果当前标记的值与total标记的值相同,如果不相同,则向当前标记的值添加某个值(例如+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>
这段代码的问题是,它在
元素.getElementsByTagName(…
处抛出NullPointerException。我已经打印出变量
current
total
的值,它们显示了正确的值,但是当我在
if(current
,对于user1和user2,它始终为true,而不是false true

最后,它应该是这样的:

<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>

测试1
50
50
测试2
50
30

问题在于,当前和总计的比较是在填充两个变量的循环内进行的。因此,比较是在只填充一个变量后进行的。如果
在循环外,则需要将
移动,最好检查两个变量是否都设置为大于零的值(假设此值不能作为输入)

int电流=0;
int-total=0;
对于(int x=0;x0&&total>0&¤t
<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>
        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));
        }