ranjan
ranjan

Reputation: 1767

Edit a data in a xml file using XMLConfiguration()

i little confused how to edit an xml content, e.g. I have an xml file

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

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

how can I edit the "xyz" to "stu"

I tried to use XMLConfiguration of commons-configuration-1.6.jar

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

Upvotes: 0

Views: 410

Answers (3)

Karuna
Karuna

Reputation: 739

Inorder to edit "xyz" to "stu" and display xml

public class DataChange {

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());
}

}

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272417

I think you need

steps.step(1).step2

in order to identify the second step node. See this doc for more info. Note that it indexes from 0, not 1 (unlike, say, XPath).

Upvotes: 1

vels4j
vels4j

Reputation: 11298

Try this

XMLConfiguration config = new XMLConfiguration("config.xml");
config.addProperty("steps.step(2).step2",tochange);

Upvotes: 0

Related Questions