|
From: Mohit A. <moh...@gm...> - 2012-10-27 04:56:25
|
For every text node I need to provde a complete xpath, this xpath goes as
column in the database.
On Fri, Oct 26, 2012 at 7:25 PM, <jz...@xi...> wrote:
> 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 <moh...@gm...>
>
> To:
> <vtd...@li...>
> 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:
>
> <a>
> <b>
> <c>
> <d>value<d>
> .....
>
> 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();
> vg.setDoc(b);
> vg.parse(true);
> log.info("File found");
> VTDNav vn = vg.getNav();
> int idx = -1, type = -1;
> StringBuilder sb = new StringBuilder();
> Map<String, ArrayList<String>> mapList = new HashMap<String,
> ArrayList<String>>();
> for (int i = 0; i < vn.getTokenCount(); i++) {
> type = vn.getTokenType(i);
> // log.info("Element name ==> " + vn.toString(i) + " Token Type "
> // + vn.getTokenType(i));
> if (type == VTDNav.TOKEN_STARTING_TAG) {
> log.info("Element name ==> " + vn.toString(i));
> sb.append(vn.toString(i) + "/");
> if (vn.contains(i, "value")) {
> do {
> i++;
> // log.info("Text Element name ==> " + vn.toString(i)
> // + " Token Type " + vn.getTokenType(i));
> } while (i < vn.getTokenCount()
> && (vn.getTokenType(i) != VTDNav.TOKEN_CHARACTER_DATA)
> && (vn.getTokenType(i) != VTDNav.TOKEN_STARTING_TAG));
> if (i < vn.getTokenCount()
> && vn.getTokenType(i) == VTDNav.TOKEN_CHARACTER_DATA) {
> // log.info(" text node ==>" + vn.toString(i) + " ");
> addInMap(sb.toString(), vn.toString(i), mapList);
> log.info("Path: " + sb.toString());
> sb.setLength(0);
> }
> }
> }
> }
> display(mapList);
> }
> private void display(Map<String, ArrayList<String>> mapList) {
> for (String key : mapList.keySet()) {
> log.info("XPath => " + key + " values =>" + mapList.get(key));
> }
> }
>
>
|