java - XPath check for attribute and show the value -
i need check bunch of .xml
files specific permission, attribute. if there such permission attribute have find out value attribute has.
this code produces nullpointerexceptions
:
public static void checkxmlpermissions(string path) { fileinputstream file; try { file = new fileinputstream(new file(path)); documentbuilderfactory builderfactory = documentbuilderfactory.newinstance(); //xpath initialisieren; documentbuilder builder = builderfactory.newdocumentbuilder(); document xmldocument = builder.parse(file); xpath xpath = xpathfactory.newinstance().newxpath(); string expression ="//*[@permission]"; //expression; nodelist nodelist = (nodelist) xpath.compile(expression).evaluate(xmldocument, xpathconstants.nodeset); (int = 0; < nodelist.getlength(); i++) { string match = nodelist.item(i).getfirstchild().getnodevalue(); system.out.println(match); } } catch(exception ex) { system.out.println(ex); } }
i guess mistake nodelist
can´t find solution own.
change:
string match = nodelist.item(i).getfirstchild().getnodevalue();
to:
string match = nodelist.item(i).gettextcontent();
Comments
Post a Comment