Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Using // >Thread Next - Re: Using // Re: Using //To: NULL Date: 6/16/2009 1:54:00 PM JS wrote:
> From Java I am trying to print values for all elements where ID = "A":
>
> public void test(){
> javax.xml.xpath.XPathFactory factory =
> javax.xml.xpath.XPathFactory.newInstance();
> javax.xml.xpath.XPath xpath = factory.newXPath();
> javax.xml.xpath.XPathExpression expression;
> String result;
> try {
> expression = xpath.compile("//content[@id =\"A\"]/@value");
> URL url = this.getClass().getClassLoader().getResource("test.xml");
> String path = url.toURI().getPath();
> result = expression.evaluate(new org.xml.sax.InputSource(path));
> System.out.println("result is = " + result);
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
>
>
> But only:
>
> 0_A
>
> is printed. Does "//" not mean that all 'content' elements are chosen?
There is an overload
http://xml.apache.org/xalan-j/apidocs/javax/xml/xpath/XPathExpression.html#evaluate(org.xml.sax.InputSource,%20javax.xml.namespace.QName)
of the evaluate method where the second argument allows you to choose a
result different from a string so use that to select
javax.xml.xpath.XPathConstants.NODESET as the return type, then I think
you can cast the returned object to a DOM NodeList and iterate over that
node list to access all selected nodes.
The method you currently use converts the selected node-set to a string
and that, with XPath 1.0, means you always get the string value of the
first node in the node-set.
--
Martin Honnen
http://msmvps.com/blogs/martin_honnen/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
