How to generate Xpath for a node in XML using java? -
i have piece of code generate xpath node. doesn't create array structure of it. example, if element has 2 elements same name, need provide index point them appropriately. illustration of below.
<abc> <def> </hij> </def> <def> </lmn> </def> </abc>
now, xpath hij
, need this:
//abc[1]/def[1]/hij
to xpath lmn
, need this:
//abc[1]/def[2]/lmn
i have piece of code give me //abc/def/hij
, //abc/def/lmn
private string getxpath(node root, string elementname) { (int = 0; < root.getchildnodes().getlength(); i++) { node node = root.getchildnodes().item(i); if (node instanceof element) { if (node.getnodename().equals(elementname)) { return "\\" + node.getnodename(); } else if (node.getchildnodes().getlength() > 0) { if(map.containskey(node.getnodename())) map.put(node.getnodename(), map.get(node.getnodename())+1); else map.put(node.getnodename(), 1); this.xpath = getxpath(node, elementname); if (this.xpath != null){ return "\\" + node.getnodename() +"["+map.get(node.getnodename())+"]"+ this.xpath; } } } } return null; }
can me append array structure this?
<abc> <def> </hij> </def> <def> </lmn> </def> </abc>
in here. closing
</hij>
and
</lmn>
without opening them. if opening them before abc, them can't close them inside abc. basically: can't intertwince them. open one, open second, close second, close one. never in way around.
this might lead error
Comments
Post a Comment