簡體   English   中英

使用DOM解析器進行大型XML拆分,但我想使用stax解析器,因此性能會提高

[英]large XML splitting using DOM parser but i want to use stax parser so the performance increase

{

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


}

嗨,我正在使用DOM解析器拆分大型XML但是拆分XML花費了很長時間。有人可以通過stax parser來幫助我這樣做嗎?它也不會生成新文件。這也是一個問題。提前如果有人可以做得到,請幫助我。

這是在vtd-xml中分割xml的代碼...

import com.ximpleware.*;
import java.io.*;

public class splitter {
    public static void main(String[] s) throws VTDException, IOException {
        VTDGen vg = new VTDGen();
        if (!vg.parseFile("input.xml", false))
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("//transaction");
        int i=0,j=0,k=0;
        File f = new File("transactionList"+k+".xml");
        FileOutputStream fos = new FileOutputStream(f);
        byte[] head="<transactions>\n".getBytes();
        byte[] tail="\n</transactions>".getBytes();
        fos.write(head);
        while((i=ap.evalXPath())!=-1){
            long l=vn.getElementFragment();
            fos.write(vn.getXML().getBytes(), (int)l, (int)(l>>32));
            j++;
            if ((j+1)%2000==0){
                k++;
                fos.write(tail);
                fos.close();
                f = new File("transactionList"+k+".xml");
                fos = new FileOutputStream(f);
                fos.write(head);
            }
        }
        fos.write(tail);
        fos.close();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM