0

I followed the way of this post to select all elements behind divider element, but it is slow with large xml file. XPath select all elements between two specific elements

My code :

 for (int i = 1; i <= countDivider; i++) {
                 NodeList listmember =(NodeList) xpath.evaluate("/*/p[count(preceding-sibling::divider)="+i+"]",
                    doc, XPathConstants.NODESET);
    }

XML file demo:

doc>
    <divider />
    <p>text</p>
    <p>text</p>
    <p>text</p>
    <p>text</p>
    <p>text</p>
    <divider />
    <p>text</p>
    <p>text</p>
    <divider />
    <p>text</p>
    <divider />
</doc>

I used /*/p[count(preceding-sibling::divider)=i] but it is slow with large xml file(1000 <divider> and 12k <p>).

  • Did you try Dimitre's example of the Kayessian method in the linked question? It should look something like: `xpath.evaluate("/*/divider["+i+"]/following-sibling::p[count(.|/*/divider["+i+"+1]/preceding-sibling::p) = count(/*/divider["+i+"+1]/preceding-sibling::p)]")` – Daniel Haley Dec 03 '20 at 19:06
  • Hmm...I mocked up a larger example file (4,864 `divider` elements and 9,728 `p` elements) and the Kayessian method was slower than your original method (20 seconds vs 4 seconds when `i` = 1234). You'll definitely need to test with your processor and see what the difference is. – Daniel Haley Dec 03 '20 at 19:33
  • Hi @DanielHaley, This is just my example xml file, the xml file I'm using is more complicated and takes a lot of time to run an xpath query. – Nguyễn Hoàng Nam Dec 04 '20 at 02:29
  • Ok but did you try the other method? Was it also slow? – Daniel Haley Dec 04 '20 at 02:30
  • yes, it also slow – Nguyễn Hoàng Nam Dec 04 '20 at 03:27

0 Answers0