java - xpath not accepting this expression -
hello have on question xpath
/abcd/nsanity/component_details[@component="ucs"]/command_details[<*configscope inhierarchical="true" cookie="{cookie}" dn="org-root" */>]/collected_data
i want retrieve string above xpath statement when giving xpath xpath expression evaulate throwing exception like
caused by: javax.xml.transform.transformerexception: location path expected, following token encountered: <configscope
the bold part in xpath expression not valid predicate expression. can guess, want achieve. if want <command_details/> elements, have <configscope/> child element attributes set inhierarchical="true", cookie="{cookie}" , dn="org-root" xpath expression should be:
/abcd/nsanity/component_details[@component='ucs']/command_details[configscope[@inhierarchical='true' , @cookie='{cookie}' , @dn='org-root']]/collected_data here example xml:
<abcd> <nsanity> <component_details component="ucs"> <command_details> <configscope inhierarchical="true" cookie="{cookie}" dn="org-root" /> <collected_data>yes</collected_data> </command_details> <command_details> <configscope inhierarchical="true" cookie="{cookie}" dn="xxx"/> <collected_data>no</collected_data> </command_details> </component_details> </nsanity> </abcd> the following java program reads xml file test.xml , evaluates xpath expression (and prints text node of element <collected_data/>.
import javax.xml.parsers.documentbuilderfactory; import javax.xml.xpath.xpath; import javax.xml.xpath.xpathconstants; import javax.xml.xpath.xpathfactory; import org.w3c.dom.document; import org.w3c.dom.element; import org.w3c.dom.nodelist; public class test { public static void main(string[] args) throws exception { documentbuilderfactory dbf = documentbuilderfactory.newinstance(); document document = dbf.newdocumentbuilder().parse("test.xml"); xpath xpath = xpathfactory.newinstance().newxpath() ; nodelist nl = (nodelist) xpath.evaluate("/abcd/nsanity/component_details[@component='ucs']/command_details[configscope[@inhierarchical='true' , @cookie='{cookie}' , @dn='org-root']]/collected_data", document, xpathconstants.nodeset); for(int = 0; < nl.getlength(); i++) { element el = (element) nl.item(i); system.out.println(el.gettextcontent()); } } }
Comments
Post a Comment