From: Nil R. S. <nil...@ya...> - 2016-10-05 20:49:49
|
try this approach public List<String> evaluateToNodeList(String xp, VTDNav vn) throws Exception { List<String> lns = new ArrayList<String>(); AutoPilot ap = new AutoPilot(vn); ap.selectXPath(xp); int curr = vn.getCurrentIndex(); while (ap.evalXPath() != -1) { lns.add(getValue(vn, vn.getCurrentIndex())); } vn.recoverNode(curr); ap.resetXPath(); return lns; } public String getValue(VTDNav vNode, int cur) { String value = null; // This is to restore back VTDNav if we re use across different objects. int _cur = vNode.getCurrentIndex(); try { vNode.recoverNode(cur); vNode.push(); /* * extracting entire all text content of node (along with child nodes text) */ if (vNode.getTokenType(cur) == VTDNav.TOKEN_STARTING_TAG) { value = vNode.getXPathStringVal(); } else if (vNode.getTokenType(cur) == VTDNav.TOKEN_ATTR_NAME) { value = vNode.toNormalizedString(cur + 1); } else { int t = vNode.getText(); // get the index of the text (char data or CDATA) if (t != -1) { value = vNode.toNormalizedString(t); } } } catch (NavException e) { } // This is to restore back VTDNav if we re use across different objects. try { vNode.recoverNode(_cur); } catch (Exception e) { } return value; } On Wednesday, 5 October 2016 8:09 AM, "al...@sa..." <al...@sa...> wrote: Is there a way to avoid having to call setDoc and parse each time we need to use VTDNav? We want to create one VTDGen object per XML document and re-use it for xpath queries. Right now, we are loading the VTDGen object each time we need to run a query, like this: VTDGen vtdGen = new VTDGen(); vtdGen.setDoc(xmlBytes); vtdGen.parse(false); VTDNav vtdNav = vtdGen.getNav(); Instead, we would like to pass the vtdGen object to different methods and not call setDoc() and parse() every time. The problem occurs with VTDNav - it seems that it must be created right after calling VTDGen.parse(). Otherwise we get an exception "Value does not fall within the selected range" when using vtdGen.getNav() We are using VTDXML 2.11 in C# and Java. (because v2.12 only works with Java) Thanks, Al ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot _______________________________________________ Vtd-xml-users mailing list Vtd...@li... https://lists.sourceforge.net/lists/listinfo/vtd-xml-users |