Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Re: Using // >Thread Next - Re: Using // Re: Using //To: NULL Date: 6/16/2009 2:29:00 PM
"Martin Honnen" <mahotrash@y...> wrote in message
news:4a3787e8$0$30232$9b4e6d93@n......
> 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 closest I coud find was NodeList:
NodeList list = (NodeList) expression.evaluate(new
org.xml.sax.InputSource(path), javax.xml.xpath.XPathConstants.NODESET);
for (Object obj : list) {
System.out.println((String)obj);
}
But that object is not iterable.
>
> 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.
Ok but if I use XPath 2.0 would I then get the full result set?
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
