XMLModifier for multiple modifications

  • Matt Nelson

    Matt Nelson - 2009-01-22


    I have an XML file that I would like to run multiple XPath Expressions against - removing any elements that match these expressions.  I get an error telling me that on the second pass i am trying to remove from an illegal offset value. 

    This is probably due to the first pass removing some of the values that are also trying to be removed by the second pass.

    What is the best (quickest) way to achieve what I am trying to do.  Currently after each pass I get the XMLModifier to output into a ByteArrayOutputStream and then re-load and re-parse the output into tthe VTDGen.

    My current code is as follows

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLModifier xm = new XMLModifier();
    VTDGen vg = new VTDGen();
    VTDNav vn;

    AutoPilot ap1 = getAutoPilot(xPath1);
    if (vg.parseFile("resources/EER_data-v-2-0.xml", true)) {
                vn = vg.getNav()
                while ((i = ap1.evalXPath()) != -1) {
                    // remove the cursor element in the embedded VTDNav object
                xm.output(bos );

    AutoPilot ap2 = getAutoPilot(xPath2);
    vn = vg.getNav();
    while ((ap2.evalXPath()) != -1) {


    Also, the output file has a lot of whitespace in it, is there any way of collapsing this?

    • jimmy zhang

      jimmy zhang - 2009-01-22

      It is difficult to determine the exact cause by your example.
      If you are trying to move the same tokens multiple time, exceptions will
      be thrown.
      If you are trying to remove an element (start tag) whose parent fragment
      is already removed, you will also get exception.

      can you join the vtd-xml-users mailing list and email your code sample to me
      so I can take a further look?

    • Matt Nelson

      Matt Nelson - 2009-01-23


      I have determined the cause is that on the second pass I am trying to remove an element whose parent fragment was already removed in the first pass. 

      What I would like to do is have the XMLModifier update the VTDNav, so that the second pass is working off the updated document, and therefore this error won't happen.  The only way I can see to do this is to have the XMLModifier output to an output stream, and then load the contents of the output stream back into the VTDNav and then re-parse it.

      So between each pass I have:
      xm.output(bos);      //Output my modified XML to an output stream
      vg.setDoc_BR(bos.toByteArray());  //Set the modified XML back onto the VTDGen
      vg.parse(true);       //Re parse the document
      vn = vg.getNav()    //Re-create the navigator
      ap.bind(vn);           //Bind my XPath to the new navigator

      Is this the most efficient way of processing this?  Or is there a quicker technique? 

    • jimmy zhang

      jimmy zhang - 2009-01-23

      If the cause of exception is overlapping offset value of two tokens... would it be possible to rewrite those xpaths to avoid overlap...

      early in the design that we feel the case of overlapping offset problem can be solved in other means...

      one of the those ways is described in this article:


      let me know if it makes sense or not..


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks