简体   繁体   English

VTD-XML XPath跳过了前3条记录

[英]VTD-XML XPath is skipping first 3 records

I am using VTD-XML to split a large xml file into smaller xml files. 我正在使用VTD-XML将较大的xml文件拆分为较小的xml文件。 Everything works great accept the: 一切正常,请接受:

autoPilot.selectXPath("//nodeName")

It is skipping over the first 3 nodes for some reason. 由于某种原因,它跳过了前三个节点。

EDIT: vtd-xml-author pointed out that LOG.info("xpath has found "+ ap.evalXPath() +" items"); 编辑:vtd-xml-author指出LOG.info("xpath has found "+ ap.evalXPath() +" items"); does not return the count but returns the node index. 不返回计数,但返回节点索引。

The new split xml file is missing the first three nodes from the original file. 新的分割xml文件缺少原始文件的前三个节点。

Here is basic XML layout. 这是基本的XML布局。 I can't display the true xml data but here is what it looks like: 我无法显示真实的xml数据,但它看起来像这样:

<rootNode>
          <parentNode>
                      <contentNode>..children inside...</contentNode>
                      <contentNode>..children inside...</contentNode>
                      <contentNode>..children inside...</contentNode>
                      <contentNode>..children inside...</contentNode>
          </parentNode>
</rootNode>

And here is the function i am using to split the xml: 这是我用来分割xml的函数:

public void splitXml(String parentNode, String contentNodes)throws Exception {
    LOG.info("Splitting " + outputName + parentNode);
    VTDGen vg = new VTDGen();   

     if (vg.parseFile(xmlSource, true)){

        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("//"+contentNode);

        int i=-1;
        int k=0;
        byte[] ba = vn.getXML().getBytes();
        FileOutputStream fos = getNewXml(parentNode);
        while((i=ap.evalXPath())!=-1){

            if(fos.getChannel().size() > maxFileSize){
                finishXml(fos,contentNode);
                LOG.info("Finished file with " + k + "nodes");
                fos = getNewXml(contentNode);
                k=0;
            }
            k++;
            long l = vn.getElementFragment();
            fos.write(ba, (int)l, (int)(l>>32));
            fos.write("\n".getBytes());
        }
        finishXml(fos,contentNode);
        LOG.info("Finished Splitting " + outputName + " " + parentNode + " with " +k+ " nodes");
    } else {
        LOG.info("Parse Failed");
    }


}

Edit: added in counter to while loop. 编辑:添加到while循环的计数器中。

as vtd-xml-author suggested i added in the counter to the while loop. 正如vtd-xml-author建议的,我在计数器中添加了while循环。

        while((i=ap.evalXPath())!=-1){
            // if filesize is at max create a new File
            if(fos.getChannel().size() > maxFileSize){
                finishXml(fos,contentNode);
                LOG.info("Finished file with " + k + "nodes");
                fos = getNewXml(contentNode);
                k=0;

            }
            k++;
            long l = vn.getElementFragment();
            fos.write(ba, (int)l, (int)(l>>32));
            fos.write("\n".getBytes());
        }

The first time i ran it the output was only missing 1 record. 我第一次运行它时,输出仅缺少1条记录。 I then deleted the output xml files and the folder and re-ran it the splitter. 然后,我删除了输出xml文件和文件夹,然后重新运行了分离器。 This time it came back with the correct number in the log and correctly split the files. 这次,它以正确的编号返回日志,并正确分割了文件。 I repeated the process numerous times while deleting the created folder and files and also without deleting the files. 在删除创建的文件夹和文件时,我也重复了多次,同时也没有删除文件。 I got the same correct results every time. 我每次都得到相同的正确结果。 I am guessing that the IDE or something wasn't refreshing correctly. 我猜IDE或某些东西没有正确刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM