Xpath expression for getting an attribute value fails in Java -
i trying attribute value xml file, code fails exception below:
11-15 16:34:42.270: debug/xpathutil(403): exception = javax.xml.xpath.xpathexpressionexception: javax.xml.transform.transformerexception: illegal tokens: '@', 'source'
here code use node list:
private static final string xpath_source = "array/extconsumer@source"; mdocument = xpathutils.createxpathdocument(xml); nodelist fullnamenodelist = xpathutils.getnodelist(mdocument, xpath_fullname);
and here xpathutils
class:
public class xpathutils { private static xpath xpath = xpathfactory.newinstance().newxpath(); private static string tag = "xpathutil"; public static document createxpathdocument(string xml) { try { log.d(tag , "about create document builder factory"); documentbuilderfactory docfactory = documentbuilderfactory .newinstance(); log.d(tag , "about create document builder "); documentbuilder builder = docfactory.newdocumentbuilder(); log.d(tag , "about create document parsing xml string is: "); log.d(tag ,xml ); document document = builder.parse(new inputsource( new stringreader(xml))); log.d(tag , "if see message everythings fine "); return document; } catch (exception e) { e.printstacktrace(); log.d(tag , "exception occured here " + e.tostring()); return null; } } public static nodelist getnodelist(document doc, string expr) { try { log.d(tag , "inside getnodelist"); xpathexpression pathexpr = xpath.compile(expr); return (nodelist) pathexpr.evaluate(doc, xpathconstants.nodeset); } catch (exception e) { e.printstacktrace(); log.d(tag, "exception = " + e.tostring()); } return null; } // extracts string value given expression public static string getnodevalue(node n, string expr) { try { log.d(tag , "inside getnodevalue"); xpathexpression pathexpr = xpath.compile(expr); return (string) pathexpr.evaluate(n, xpathconstants.string); } catch (exception e) { e.printstacktrace(); } return null; }
i exception thrown in getnodelist
method.
now, according http://www.w3schools.com/xpath/xpath_syntax.asp, attribute value, use "@" sign. reason, java complaining symbol.
try
array/extconsumer/@source
as xpath expression. selects source attribute of extconsumer element.
Comments
Post a Comment