I'm reading correctly an xml file, but I'm not able to write it.
Here is the file: a configuration file for key-value settings.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<setting key="Password" value="d92e1dedba95d2cf00d4c567e57e3342"/>
<setting key="ExceptionFileLog" value="exception.txt"/>
<setting key="ActionFileLog" value="actions.txt" />
<setting key="ShowInfoMessage" value="false" />
</configuration>
I correctly open and read file using javax.xml.parsers.DocumentBuilder:
private Document _doc = null;
public XmlConfig(String filePath) throws Exception
{
File xml = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
_doc = dBuilder.parse(xml);
_doc.getDocumentElement().normalize();
}
So far so good, but I'm not able to write and persist changes to the file:
public boolean updateValue(String key, String value)
{
NodeList settlist = _doc.getElementsByTagName(SETTNAME);
for(int i = 0; i < settlist.getLength(); i++)
{
Element sett = (Element) settlist.item(i);
if(sett.getNodeType() == Node.ELEMENT_NODE)
{
if(null != sett.getAttribute("key") && sett.getAttribute("key").equals(key))
{
sett.setAttribute("value", value);
return true;
}
}
}
return false;
}
So, if I print xml file from _doc (Document object) the changes are correctly written, but xml file is not updated!
I suppose that I'm opening,reading and writing xml file in memory and I need a way to persist changes on disk. I have no idea, any suggestion will be appreciated.