So I tried it with XPathExpressionEngine
but it's not working.
My XML file looks like this.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<omg>
<configurationCreated>24/05/20 00:43:42</configurationCreated>
</omg>
I want the end result to be this.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<omg>
<configurationCreated>24/05/20 00:43:42</configurationCreated>
<newElement />
</omg>
I tried this.
Parameters parameters = new Parameters();
FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class)
.configure(parameters
.xml()
.setFileName("mahfile.xml")
.setExpressionEngine(new XPathExpressionEngine()));
builder.setAutoSave(true);
try {
configuration = builder.getConfiguration();
} catch (ConfigurationException e) {
return;
}
configuration.addProperty("omg", "newElement");
The result however is this (what the actual heck).
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<omg>
<configurationCreated>24/05/20 00:43:42</configurationCreated>
<omg>newElement</omg>
</omg>
I also tried the following XPath expressions with addProperty() method but nothing is working. How do I do this correctly? Documentation is not helpful here.
configuration.addProperty("/omg", "newElement");
configuration.addProperty("//omg", "newElement");
configuration.addProperty("newElement", null);
configuration.addProperty("omg newElement", null);
I think this is not possible, however an expression like configuration.addProperty("newElement/childElement/name", "value")
creates the new element but with a child in it.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<omg>
<configurationCreated>24/05/20 00:43:42</configurationCreated>
<newElement>
<childElement>
<name>value</name>
</childElement>
</newElement>
</omg>
On a side note: I additionally tried configuration.getDocument().createElement("newElement")
but this does not save my configuration file automatically.