XmlConfigPath xmlConfigPath = setting.getXmlConfigPath();
Map<String, String> contents = xmlConfigPath.getContents();
//目标xml文件路径
String xmlFilePath = setting.getInstallPath() + "/" + xmlConfigPath.getXmlFileName();
//目标文件对象
File file = new File(xmlFilePath);
XMLWriter writer = null;// 声明写XML的对象
if (file.exists()) {
try {
SAXReader reader = new SAXReader();
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");// 设置XML文件的编码格式
Document document = reader.read(file);
Element root = document.getRootElement();
List<String> nodeNames = xmlConfigPath.getNodeNames();
for (String s : nodeNames) {
Element dataSource = parse(root , "id" , s);
if (dataSource == null) {
throw new RuntimeException("在文件:"+xmlFilePath+",中未找到id="+s+"的标签");
}
//清空bean节点
dataSource.clearContent();
String xmlText = contents.get(s).replaceAll("\n|\t", "");
//将xml文本转换为document对象
Document parseText = DocumentHelper.parseText("<myroot>" + xmlText.trim() + "</myroot>");
Element rootElement = parseText.getRootElement();
//将文本节点写入目标xml的节点下
dataSource.setContent(rootElement.content());
writer = new XMLWriter(new FileWriter(file), format);