Search code examples
xmlvtd-xml

Unexpected NodeRecorder behaviour


Given the following XML doc:

<?xml version="1.0" encoding="UTF-8"?>
<InlinePurchaseOrder>
   <largeMessage>
      <BuyersID>20031234-1</BuyersID>
      <IssueDate>2003-01-23</IssueDate>
      <LineExtensionTotalAmount amountCurrencyID="USD" amountCurrencyCodeListVersionID="0.3">438.50</LineExtensionTotalAmount>
      <BuyerParty>
         <Party>
            <PartyName>
               <Name>Bills Microdevices</Name>
               <Name>BM</Name>
               <Name>BMic</Name>
            </PartyName>
         </Party>
      </BuyerParty>
   </largeMessage>
</InlinePurchaseOrder>

I am issuing an XPath to navigate to the <PartyName> node, and then I want to issue a second XPath to get all of the <Name> nodes, and record their positions.

For some reason, NodeRecorder.iterate() is immediately returning -1, even though the nodes were properly recorded, I called resetPointer(), and I am moving in the forward direction. I had other docs with this type of logic (but different XPaths) which worked, but this one isn't. Any ideas?

Demo code:

File f = new File("noderecorder.xml");
FileInputStream fis = new FileInputStream(f);
byte[] b = new byte[(int) f.length()];
fis.read(b);
fis.close();

VTDGen vg = new VTDGen();
vg.setDoc(b);
vg.parse(false);

VTDNav vn = vg.getNav();

AutoPilot partyNameXPath = new AutoPilot(vn);
partyNameXPath.selectXPath("largeMessage/BuyerParty/Party/PartyName");

// Find PartyName
while (partyNameXPath.evalXPath() != -1) {
    System.out.println("Found: " + vn.getCurrentIndex() + " " + 
                       vn.toRawString(vn.getCurrentIndex()));

    NodeRecorder nr = new NodeRecorder(vn);

    // Find all Names and record their positions
    AutoPilot nameXPath = new AutoPilot(vn);
    nameXPath.selectXPath("Name");            

    while (nameXPath.evalXPath() != -1) {
        System.out.println("Found: " + vn.getCurrentIndex() + " " +
                           vn.toRawString(vn.getCurrentIndex()));
        nr.record();
    }

    nr.resetPointer();

    System.out.println("Index before nr.iterate: " + vn.getCurrentIndex());

    // Do some other work...

    // Now go and iterate over the nodes we recorded

    while (nr.iterate() != -1) {
        System.out.println("Re-found: " + vn.getCurrentIndex() + " " + 
                           vn.toRawString(vn.getCurrentIndex()));
    }

    System.out.println("Index after nr.iterate: " + 
                       vn.getCurrentIndex());

}

Output:

Found: 19 PartyName
Found: 20 Name
Found: 22 Name
Found: 24 Name
Index before nr.iterate: 19
Index after nr.iterate: -1

Edit: Just tried a modified document with less nesting and this one works (changing partyNameXPath to just "PartyName"), the problem seems to be depth related:

<?xml version="1.0" encoding="UTF-8"?>
<InlinePurchaseOrder>
     <PartyName>
        <Name>Bills Microdevices</Name>
        <Name>BM</Name>
        <Name>BMic</Name>
     </PartyName>
</InlinePurchaseOrder>

Output:

Found: 6 PartyName
Found: 7 Name
Found: 9 Name
Found: 11 Name
Index before nr.iterate: 6
Re-found: 7 Name
Re-found: 9 Name
Re-found: 11 Name
Index after nr.iterate: 11

Solution

  • This is a bug that has just been fixed. Thanks for reporting. Could you check out NodeRecorder.java from CVS and give it a try again?