0

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

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

<step>
    <step1>pqr</step1>
    <step2>xyz</step2>
</step>
</steps>
</configuration>

如何将“xyz”编辑为“stu”

我尝试使用 commons-configuration-1.6.jar 的 XMLConfiguration

setProp(String name, String tochange){ // here I pass name as  "pqr" , toChange as "stu"
      XMLConfiguration config = new XMLConfiguration("config.xml");
      //TODO: config.setProperty("steps.step.step2",tochange); Here I am not sure what to do..
}
4

3 回答 3

1

我想你需要

steps.step(1).step2

以识别第二步节点。有关更多信息,请参阅此文档。请注意,它从 0 开始索引,而不是 1(与 XPath 不同)。

于 2013-01-14T10:40:24.473 回答
0

尝试这个

XMLConfiguration config = new XMLConfiguration("config.xml");
config.addProperty("steps.step(2).step2",tochange);
于 2013-01-14T10:48:37.673 回答
0

为了将“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());
}

}

于 2015-10-02T02:33:35.453 回答