xml结构:
<factory id= "1">
<hall id= "101">
<line id= "10101">
</line>
想要添加新元素,位置在线下。并成为:
<factory id= "1">
<hall id= "101">
<line id= "10101">
<location id= "123"></location>
</line>
的java
String path;
Document doc = null;
path = "./webapp/WEB/config/aaaaa.xml";
try {
SAXReader reader = new SAXReader();
doc = reader.read(path);
} catch (DocumentException e) {
e.printStackTrace();
}
String insertPointPath = "//abc/ddd/factory[@id='1']/hall[@id='101']/line[@id='10101']";
Element insertPoint = (Element) doc.selectSingleNode(insertPointPath);
insertPoint.addElement("location").addAttribute("id", "123");
新元素添加在“line”节点下,在调试期间观察到。 但是,该文件没有改变。新元素不存在。
为什么更改未保存在文档中?