|
From: Mohit A. <moh...@gm...> - 2012-10-26 21:20:19
|
>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));
}
}
|