{
public class XmlSplit {
public static void main(String [] args) throws Exception {
File input = new File("C:\\Users\\Edit5.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = (Document) dbf.newDocumentBuilder().parse(input);
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xpath.evaluate("//transaction", doc, XPathConstants.NODESET);
int itemsPerFile = 2000;
int fileNumber = 0;
Document currentDoc = (Document) dbf.newDocumentBuilder().newDocument();
Node rootNode;
rootNode = currentDoc.createElement("transactions");
File currentFile = new File(fileNumber+".xml");
for (int i=1; i <= nodes.getLength(); i++) {
Node imported = currentDoc.importNode(nodes.item(i-1), true);
rootNode.appendChild(imported);
if (i % itemsPerFile == 0) {
writeToFile(rootNode, currentFile);
rootNode = currentDoc.createElement("transactions");
currentFile = new File((++fileNumber)+"C:\\UsersEdit1.xml");
}
else
{
writeToFile(rootNode, currentFile);
}
}
}
private static void writeToFile(Node node, File file) throws Exception {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(node), new StreamResult(new FileWriter("C:\\UsersEdit1.xml")));
}
}
}
Hi I am splitting an large XML
using DOM
parser but it is taking long time to split the XML
.Can someone help me with this to do this using stax parser.Also it is not generating new file .That is also a problem .THanks In advance If someone can do this please help me .