{
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")));
}
}
}
您好我正在使用XML
解析器拆分大型DOM
,但需要花费很长时间才能拆分XML
。有人可以帮助我使用stax解析器执行此操作。也可以它没有生成新文件。这也是一个问题。预先提醒如果有人能做到这一点,请帮助我。
答案 0 :(得分:0)
以下是在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();
}
}