我们有一个请求 xml,并且希望批量处理该请求 xml。
示例 1:
<ApplicationArea>
---
---
</ApplicationArea>
<DataArea>
<Employ>
<eno></eno>
<employName></employName>
</Employ>
<Employ>
<eno></eno>
<employName></employName>
</Employ>
<Employ>
<eno></eno>
<employName></employName>
</Employ>
<Employ>
<eno></eno>
<employName></employName>
</Employ>
<Employ>
<eno></eno>
<employName></employName>
</Employ>
<Employ>
<eno></eno>
<employName></employName>
</Employ>
</DataArea>
在此请求中,使用记录没有限制。假设请求 xml 中有 1000 个 Employs,在这种情况下我想分成多个 xml,批量大小为 100。我的意思是,想要创建 10 个 xml,每个 xml 包含相同的 <ApplicationArea>
但是<DataArea>
应仅包含 50 个雇员。
第一个 xml 应包含 1-100 个雇员。第二个 xml 应包含 101-200 个员工
<小时/>
我怎样才能实现这个目标。请提供建议。
最诚挚的问候,拉维
使用此代码(将 xml 的批量大小更改为 100)
public static void splitXML() throws Exception {
int batchSize = 2;
List<String> splittedXMLs = new ArrayList<String>();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<RootElement>\n" +
" <ApplicationArea>\n" +
" <Test />\n" +
" </ApplicationArea>\n" +
" <DataArea>\n" +
" <Employ>\n" +
" <eno />\n" +
" <employName />\n" +
" </Employ>\n" +
" <Employ>\n" +
" <eno />\n" +
" <employName />\n" +
" </Employ>\n" +
" <Employ>\n" +
" <eno />\n" +
" <employName />\n" +
" </Employ>\n" +
" <Employ>\n" +
" <eno />\n" +
" <employName />\n" +
" </Employ>\n" +
" <Employ>\n" +
" <eno />\n" +
" <employName />\n" +
" </Employ>\n" +
" <Employ>\n" +
" <eno />\n" +
" <employName />\n" +
" </Employ>\n" +
" </DataArea>\n" +
"</RootElement>";
System.out.println(xml);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new ByteArrayInputStream(xml.getBytes()));
// This is going to be our base document for new document.
Document newDoc = dBuilder.newDocument();
Node importedNode = newDoc.importNode(doc.getDocumentElement(), true);
newDoc.appendChild(importedNode);
Element dataAreaElement = (Element) newDoc.getElementsByTagName("DataArea").item(0);
removeChildren(dataAreaElement);
Document splittedDoc = (Document) newDoc.cloneNode(true);
List<Node> employList = new ArrayList<Node>();
NodeList dataArea = doc.getElementsByTagName("Employ");
for (int i = 0; i < dataArea.getLength(); i++) {
employList.add(dataArea.item(i));
if (i % batchSize == 0 || i == dataArea.getLength() - 1) {
Node dataAreaNode = splittedDoc.getDocumentElement().getElementsByTagName("DataArea").item(0);
for (Node node1 : employList) {
dataAreaNode.appendChild(splittedDoc.importNode(node1, true));
}
employList.clear();
splittedXMLs.add(convertDocumentToString(splittedDoc));
splittedDoc = (Document) newDoc.cloneNode(true);
}
}
// Printing all splitted XMLs
for (String splittedXML : splittedXMLs) {
System.out.println("##### " + splittedXML);
}
}
public static void removeChildren(Node node) {
while (node.hasChildNodes())
node.removeChild(node.getFirstChild());
}
private static String convertDocumentToString(Document doc) {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = tf.newTransformer();
// below code to remove XML declaration
// transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String output = writer.getBuffer().toString();
return output;
} catch (TransformerException e) {
e.printStackTrace();
}
return null;
}
我已经为输出 xml 创建了字符串,您可以将其写入文件或执行任何您想要的操作。
我是一名优秀的程序员,十分优秀!