I want to navigate the xml using vtd-xml, and I want to navigate to any index given by the XPath. For example:
vg.parseFile("/tmp/somexml.xml",true);
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot();
ap.selectXPath("//someelement");
ap.bind(vn);
for (int i = ap.evalXPath(); i != -1;) {
try {
vn.toElement(i);
} catch (Exception ex) {
logger.error(null, ex);
}
I debugged and for example I get the index 40, but when I try toElement(40) it fails. I keep getting invalid navigation. Is there anyway I can navigate to any element?
You can’t ask vtdNav to go to any index value, VTD-XML doesn’t work like that for the simple reason that the index is not guaranteed to be of the type of element.
There are ways for you to save/load a node position, or recover a node position from an index value. But in general a node position is not guaranteed to be a valid tree position (attribute value e.g.)…