Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.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 使用XMLConfiguration()编辑xml文件中的数据_Java_Xml - Fatal编程技术网

Java 使用XMLConfiguration()编辑xml文件中的数据

Java 使用XMLConfiguration()编辑xml文件中的数据,java,xml,Java,Xml,我有点困惑如何编辑xml内容, e、 我有一个xml文件 <configuration> <steps> <step> <step1>abc</step1> <step2>def</step2> </step> <step> <step1>pqr</step1> <step2>xyz</step2> <

我有点困惑如何编辑xml内容, e、 我有一个xml文件

<configuration>
<steps>
<step>
    <step1>abc</step1>
    <step2>def</step2>
</step>

<step>
    <step1>pqr</step1>
    <step2>xyz</step2>
</step>
</steps>
</configuration>
我想你需要

steps.step(1).step2
以便识别第二步节点。有关更多信息,请参阅。请注意,它从0而不是1索引(与XPath不同)。

试试这个

XMLConfiguration config = new XMLConfiguration("config.xml");
config.addProperty("steps.step(2).step2",tochange);
为了将“xyz”编辑为“stu”并显示xml

公共类数据更改{

public static void main(String[] args) throws ConfigurationException {
    XMLConfiguration config = new XMLConfiguration("change.xml");
    config.setProperty("steps.step(1).step2", "stu");       
    StringWriter s = new StringWriter();
    config.save(s);
    System.out.println(s.toString());
}

}

嘿,布莱恩!祝贺10万美元:)它成功了。。相反,它从0开始索引。。所以我们可以使用steps.setp(1)。step2@ranjan-我做了适当的修改。谢谢
public static void main(String[] args) throws ConfigurationException {
    XMLConfiguration config = new XMLConfiguration("change.xml");
    config.setProperty("steps.step(1).step2", "stu");       
    StringWriter s = new StringWriter();
    config.save(s);
    System.out.println(s.toString());
}