首页 > 其他 > 详细

xml合并问题,多个xml拼接

时间:2018-04-01 22:32:55      阅读:262      评论:0      收藏:0      [点我收藏+]
<payment>
    <AuditFileVersion>信息</AuditFileVersion>
    <telephone>销方</telephone>
</payment>
<payment>
    <AuditFileVersion>信息</AuditFileVersion>
    <telephone>销方</telephone>
</payment>

多个上边的xml合并为下边的xml:

<all>
<bb>
<payment>
    <AuditFileVersion>信息</AuditFileVersion>
    <telephone>销方</telephone>
</payment>
<payment>
    <AuditFileVersion>信息</AuditFileVersion>
    <telephone>销方</telephone>
</payment>
</bb>
</all>

实现如下:

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileFilter;
import java.util.List;
import java.util.regex.Pattern;

/**
* @author Cyhui.
*/
public class aaa {
public static void main(String[] args) throws Exception {
SAXReader saxReader = new SAXReader();

String tube = "F:\\xmll\\root.xml";
Document rootDoc = saxReader.read(new File(tube));
Element parent = rootDoc.getRootElement();
Element flows = parent.element("bb");
File file = new File("F:\\xmll");
if (file.exists()) {
File[] files = file.listFiles(new FileFilter() {
public boolean accept(File pathname) {
String fileName = pathname.getName();
String pattern = "^ff.*.xml$";
boolean matches = Pattern.matches(pattern, fileName);
return matches;
}
});
for (File f : files) {
Document read = saxReader.read(f);
List<Element> elements = read.getDocument().getRootElement().elements();
for (Element emt : elements) {
flows.add(emt.detach());
}
}
}
System.out.println(rootDoc.asXML());
}
}

 

xml合并问题,多个xml拼接

原文:https://www.cnblogs.com/hxjj/p/8688570.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!