阅读背景:

使用DOM解析器进行大型XML拆分但我想使用stax解析器以提高性能

来源:互联网 
{

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


}
{

public class XmlSplit {

public static void 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: