我们有一个请求xml,并希望以批处理方式处理此请求xml。
Ex 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>
在此请求中,使用reocords没有限制。
让我们说请求xml中有1000个Employ,在这种情况下我想分成xmls的数量,批量大小为100.我的意思是,想要创建10个xmls,每个xml包含相同的<ApplicationArea>
但是<DataArea>
应该只包含50个Employs。
我怎样才能做到这一点。请提供建议。
最诚挚的问候, 拉维
答案 0 :(得分:1)
使用此代码(将批量大小更改为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;
}
我为输出xmls创建了字符串,您可以将其写入文件或执行任何操作。