Java 基于重复元素将大型XML文件拆分为小块
考虑以下包含500 MB数据的XMLJava 基于重复元素将大型XML文件拆分为小块,java,xml,xml-parsing,stax,vtd-xml,Java,Xml,Xml Parsing,Stax,Vtd Xml,考虑以下包含500 MB数据的XML <?xml version="1.0" encoding="UTF-8"?> <Parents> <process Child ="A">...</process> <process Child="B">...</process> <process Child="A">...</process> <process Child="C"&
<?xml version="1.0" encoding="UTF-8"?>
<Parents>
<process Child ="A">...</process>
<process Child="B">...</process>
<process Child="A">...</process>
<process Child="C">..</process>
<process Child=...
</process>
<\Parents>
现在的问题是,我想根据同一组的子属性对一个实例拆分文件,所有“A”的子项都应该以与B、C和其他文件相同的方式在example_A.xml文件中。这是对现有代码的一个非常简单的修改。实际上有多种方法可以做到这一点。我将向您展示其中一种方法:使用VTDNav的getAttrVal方法()显式比较attr val
publicstaticvoidmain1(字符串参数[]){
试一试{
VTDGen vg=新VTDGen();
if(vg.parseFile(“C:\\..\\example.xml”,true)){
VTDNav vn=vg.getNav();
自动驾驶仪ap=新自动驾驶仪(vn);
ap.selectXPath(“/Parents/process”);
int chunk=0;
FileOutputStream fopsA=(新的FileOutputStream(“C:\\\..\\resultA”+chunk+“.xml”);
write(“\n”.getBytes());
FileOutputStream fopsB=(新的FileOutputStream(“C:\\..\\resultB”+chunk+“.xml”);
而((ap.evalXPath())!=-1){
long frag=vn.getElementFragment();
int i=vn.getAttrVal(“子”);
如果(i==-1)抛出新的NavException(“意外结果”);
if(vn.compareTokenString(i,“A”)==0){
write(vn.getXML().getBytes(),(int)frag,
(国际)(frag>>32));
}else if(vn.compareTokenString(i,“B”)==0){
write(vn.getXML().getBytes(),(int)frag,
(国际)(frag>>32));
}
块++;
}
write(“\n”.getBytes());
写入(“\n”.getBytes());
}
}捕获(例外情况除外){
例如printStackTrace();
}
问题是什么?哦,很抱歉,我真的错过了这一部分,我想根据子名称将XML拆分,这意味着expamle_A.XML应该包含……的所有属性。您似乎已经在使用XPath。那么又是什么问题呢???@sharon我无法在单个文件中对子节点进行分组,假设此文件有30个节点,Child=“A”所以它创建了30个文件,但我想要的是在一个文件中。好了,现在图片更清晰了,这应该进入问题“到目前为止你做了什么?”谢谢你解释得很好,但唯一的问题是如果我们有100个不同的child属性,那么我们需要提供100个else if条件,比如A、B、C、D等,将其写入不同的文件。它有没有通用的方式,比如我可以提供一个循环,用A读取所有属性,然后用B、C等类似的方式将其写入文件.正如你所看到的,当然有办法使这个过程自动化…但听起来更像是你的家庭作业而不是我的。同意吗?
public static void main(String args[]) {
try {
VTDGen v = new VTDGen();
if (v.parseFile("C:\\..\\example.xml", true)) {
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/Parents/child");
int chunk = 0;
while (( ap.evalXPath()) != -1) {
long frag = vn.getElementFragment();
(new FileOutputStream("C:\\....\\result" + chunk + ".xml")).write(vn.getXML().getBytes(), (int) frag,
(int) (frag >> 32));
chunk++;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main1(String args[]) {
try {
VTDGen vg = new VTDGen();
if (vg.parseFile("C:\\..\\example.xml", true)) {
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/Parents/process");
int chunk = 0;
FileOutputStream fopsA=(new FileOutputStream("C:\\....\\resultA" + chunk + ".xml"));
fopsA.write("<Parent>\n".getBytes());
FileOutputStream fopsB=(new FileOutputStream("C:\\....\\resultB" + chunk + ".xml"));
while (( ap.evalXPath()) != -1) {
long frag = vn.getElementFragment();
int i=vn.getAttrVal("Child");
if (i==-1) throw new NavException("unexpected result");
if (vn.compareTokenString(i,"A")==0){
fopsA.write(vn.getXML().getBytes(), (int) frag,
(int) (frag >> 32));
}else if (vn.compareTokenString(i,"B")==0){
fopsB.write(vn.getXML().getBytes(), (int) frag,
(int) (frag >> 32));
}
chunk++;
}
fopsA.write("</Parent>\n".getBytes());
fopsB.write("</Parent>\n".getBytes());
}
} catch (Exception ex) {
ex.printStackTrace();
}