Java DocumentBuilder不更新xml文件

时间:2022-09-11 21:48:28

I'm reading correctly an xml file, but I'm not able to write it.

我正在读一个xml文件,但是我无法写它。

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:

我使用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!

因此,如果我从_doc(Document对象)打印xml文件,则更改会正确写入,但xml文件不会更新!

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.

我想我正在打开,读取和写入内存中的xml文件,我需要一种方法来保持磁盘上的更改。我不知道,任何建议将不胜感激。

1 个解决方案

#1


0  

save the changed xml file using the following code

使用以下代码保存更改的xml文件

Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(xml); // xml is a object of File i.e. File xml = new File(filePath);
Source input = new DOMSource(_doc);
transformer.transform(input, output);

it will store the updated values in the xml file.

它会将更新的值存储在xml文件中。

reference from how-to-save-parsed-and-changed-dom-document-in-xml-file

从如何保存已解析和更改的dom-document-in-xml文件中引用

#1


0  

save the changed xml file using the following code

使用以下代码保存更改的xml文件

Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(xml); // xml is a object of File i.e. File xml = new File(filePath);
Source input = new DOMSource(_doc);
transformer.transform(input, output);

it will store the updated values in the xml file.

它会将更新的值存储在xml文件中。

reference from how-to-save-parsed-and-changed-dom-document-in-xml-file

从如何保存已解析和更改的dom-document-in-xml文件中引用