From: <jz...@xi...> - 2012-10-27 02:25:36
|
Getting the xpath representation of a node is neither easy nor unique, why dont u need it in the first place? ----- Original Message ----- From: Mohit Anchlia To: Cc: Sent:Fri, 26 Oct 2012 14:20:13 -0700 Subject:[Vtd-xml-users] Xpath using token From previous suggestion I switched using this logic: The problem is that I am not able to get full xpath down to the leaf. I am only able to get 2 paths. So if I have: value ..... I am only getting c/d/value. Is there a trick I can use here to get the entire path like a/b/c/d/value string ? Only other option I see is to fall back to recursive method. public void navigator(byte[] b) throws NavException, XPathParseException, XPathEvalException, EncodingException, EOFException, EntityException, ParseException { VTDGen vg = new VTDGen(); vgsetDoc(b); [1] vg.parse(true); [2] log.info [3]("File found"); VTDNav vn = vg.getNav(); int idx = -1, type = -1; StringBuilder sb = new StringBuilder(); Map mapList = new HashMap(); for (int i = 0; i < vn.getTokenCount(); i++) { type = vn.getTokenType(i); // log.info [4]("Element name ==> " + vn.toString(i) + " Token Type " // + vn.getTokenType(i)); if (type == VTDNav.TOKEN_STARTING_TAG) { log.info [5]("Element name ==> " + vn.toString(i)); sb.append(vn.toString(i) + "/"); if (vn.contains(i, "value")) { do { i++; // log.info [6]("Text Element name ==> " + vn.toString(i) // + " Token Type " + vn.getTokenType(i)); } while (i < vn.getTokenCount() && (vn.getTokenType(i) != VTDNav.TOKEN_CHARACTER_DATA) & if (i < vn.getTokenCount() & addInMap(sb.toString(), vn.toString(i), mapList); log.info [8]("Path: " + sb.toString()); sb.setLength(0); } } } } display(mapList); } private void display(Map mapList) { for (String key : mapList.keySet()) { log.info [9]("XPath => " + key + " values =>" + mapList.get(key)); [10] } } Links: ------ [1] http://sitemail.hostway.com/http: [2] http://sitemail.hostway.com/http: [3] http://log.info [4] http://log.info [5] http://log.info [6] http://log.info [7] http://log.info [8] http://log.info [9] http://log.info [10] http://sitemail.hostway.com/http: |