通过java往xml配置文件添加节点
<info> <booklist> <book> <name>Java设计模式</name> <price>150</price> </book> <book> <name>三国演义</name> <price>200</price> </book> </booklist> <picturelist> <pirture> <name>图片1</name> <price>100</price> </pirture> <pirture> <name>图片2</name> <price>200</price> </pirture> </picturelist></info>
<info> <booklist> <book> <name>Java设计模式</name> <price>150</price> </book> <book> <name>三国演义</name> <price>200</price> </book> <book> <name>水浒传</name> <price>300</price> </book> </booklist> <picturelist> <pirture> <name>图片1</name> <price>100</price> </pirture> <pirture> <name>图片2</name> <price>200</price> </pirture> </picturelist></info>
File file = new File("D:\\test.xml"); SAXReader reader = new SAXReader(); Document doc = reader.read(file); Element dicts = (Element) doc.selectSingleNode("/info/booklist"); Element bookElement = dicts.addElement("book"); Element name = bookElement.addElement("name"); name.addText("水浒传"); Element price = bookElement.addElement("price"); price.addText("300"); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream(file), format); writer.write(doc); writer.close();