Java 使用XMLConfiguration()编辑xml文件中的数据
我有点困惑如何编辑xml内容, e、 我有一个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> <
<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());
}