Wednesday, 15 April 2015

java - How to get attribute value of node in xml using xpath? -



java - How to get attribute value of node in xml using xpath? -

this question has reply here:

get value of attribute 1 reply

i want value of attribute name of nodes. xml is

<branches> <branch-area name="abc"> <branch> <branch-name> xyz street</branch-name> </branch> </branch-area> <branch-area name="def 11"> <branch> <branch-name>pqr</branch-name> </branch> </branch-area> <branch-area name="ghi 14"> <branch> <branch-name>jkl</branch-name> </branch> </branch-area> </branches>

for using below code

string xpathexpression = "//branch-area[@name]"; nodelist list = (nodelist) xpathfactory.newinstance().newxpath().evaluate(xpathexpression,inputsource, xpathconstants.nodeset); for(int i=0; i<list.getlength();i+=1) { system.out.println(list.item(i).getfirstchild().getnodevalue()); }

but getting empty string can please tell me wrong ?

//branch-area[@name] homecoming element nodes - branch-area elements have name attribute. if want attribute nodes use

class="lang-none prettyprint-override">//branch-area/@name

and don't need getfirstchild() in loop, attribute nodes hold value straight rather having text node children.

java xml xpath

No comments:

Post a Comment