使用VTD-XML解析大型XML文件

时间:2014-03-19 09:55:39

标签: xml vtd-xml

要在巨大的xml文件中执行XPATH查询,我阅读了许多文章,这些文章优先选择VTD-XML,因此我复制了这些文章中的代码:

import com.ximpleware.extended.*;
public class MainVTD {
    public static void main(String[] s) throws Exception{
        VTDGenHuge vg = new VTDGenHuge();
        if (vg.parseFile("./resource/init/dblp_3.xml",true,VTDGenHuge.MEM_MAPPED)){
            VTDNavHuge vnh = vg.getNav();
            AutoPilotHuge aph = new AutoPilotHuge(vnh);
            aph.selectXPath("/*/*[not(name() = name(preceding-sibling::*[1]))]");
            int i ;
            while ((i=aph.evalXPath())!=-1){
                System.out.println(" element name is "+vnh.toString(i));
            }
        }else System.out.println("Doc not mapped");
    }
}

但是当我运行它没有结果时,所以它的平均XML文件没有映射到内存中...... 我的问题是我如何强制在VTD-XML中映射xml文件?

1 个答案:

答案 0 :(得分:0)

将以下代码放入并打印出异常以查看错误...

import java.io.IOException;

import com.ximpleware.extended.*;
public class testExtendedVTD {
    public static void main(String s[]){
        VTDGenHuge vgh = new VTDGenHuge();
        XMLBuffer xb = new XMLBuffer(); //or XMLMemeMappedBuffer
        try {
            xb.readFile("test.xml");
            vgh.setDoc(xb);
        } catch (ParseExceptionHuge e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}