Speeding up xpath

后端 未结 6 620
广开言路
广开言路 2020-12-24 14:09

I have a 1000 entry document whose format is something like:


     
          
                   


        
6条回答
  •  轮回少年
    2020-12-24 14:45

    Try VTD-XML. It uses less memory than DOM. It is easier to use than SAX and supports XPath. Here is some sample code to help you get started. It applies an XPath to get the Entry elements and then prints out the n1 and n2 child elements.

    final VTDGen vg = new VTDGen();
    vg.parseFile("/path/to/file.xml", false);
    
    final VTDNav vn = vg.getNav();
    final AutoPilot ap = new AutoPilot(vn);
    ap.selectXPath("/Example/Entry");
    int count = 1;
    while (ap.evalXPath() != -1) {
        System.out.println("Inside Entry: " + count);
    
        //move to n1 child
        vn.toElement(VTDNav.FIRST_CHILD, "n1");
        System.out.println("\tn1: " + vn.toNormalizedString(vn.getText()));
    
        //move to n2 child
        vn.toElement(VTDNav.NEXT_SIBLING, "n2");
        System.out.println("\tn2: " + vn.toNormalizedString(vn.getText()));
    
        //move back to parent
        vn.toElement(VTDNav.PARENT);
        count++;
    }
    

提交回复
热议问题