请问xml的错误。一个弱弱的问题。
想问一下,在程序中,我的xml文件是listen.xml,然后我使用jdom编程修改了此文件,仍然写回listen.xml,但无法修改。写成别的文件名1.xml是可以的。
我知道可能是listen.xml是正在被使用,所以无法写进去,但我觉得如果jdom没有提供一个方便的写回xml文件的方法,则修改xml文件是比较麻烦的。比如要写一个别的文件的名字如1.xml再改回listen.xml。
我想问一下jdom中有没有好的方法,直接写回。方便大家操作。代码如下:
SAXBuilder parser = new SAXBuilder(); // 新建立构造器
try {
Document document = parser.build("listen.xml");
Element root = document.getRootElement();
Element systemId = root.getChild("systemId");
if (systemId != null) { //代表有系统信息
systemId.setText(systemInfo.getSystemId());
}else{
Element ele = new Element("systemId");
ele.setText(systemInfo.getSystemId());
root.addContent(ele);
}
XMLOutputter outp = new XMLOutputter (" ",true,"gb2312"); // 构造新的输出流
Document doc = (Document)document.clone();
// outp.output(doc, new FileOutputStream("listen.xml")); //写不进去
outp.output(doc, new FileOutputStream("1.xml")); 可以写进去