xml合并问题,多个xml拼接

<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());
}
}

 

posted @ 2018-04-01 21:58  王小剑  阅读(5165)  评论(0编辑  收藏  举报