Viola
Viola

Reputation: 487

How to print the list of the first ten data in XML

Currently I get only the first one DisplayName with Reputation > 100. How can I print the 10 first DisplayName? Like an output I want to get a list of the 10 first DisplayName with Reputation > 100.

Here is my Main file:

String output = (String) xPath.evaluate(
    "/users/row[@Reputation>'100']/@DisplayName", 
    doc2.getDocumentElement(),
    XPathConstants.STRING);
System.out.println("DisplayName for user with Reputation > 100: " + output);

Upvotes: 2

Views: 53

Answers (2)

Jamie
Jamie

Reputation: 1937

xPath.evaluate can return various object types. This is controlled by the third parameter, which you have specified as "XPathConstants.STRING".

I believe you are looking for XPathConstants.NODESET, eg.:

NodeList nodes = (NodeList)xPath.evaluate("/users/row[@Reputation>'100']/@DisplayName", doc2.getDocumentElement(), XPathConstants.NODESET);

I leave it to you to look up the API for XPath NodeSet.

Upvotes: 0

Karol Dowbecki
Karol Dowbecki

Reputation: 44960

Try the position() syntax

/users/row[@Reputation>'100' and position() < 11]/@DisplayName

as per Xpath Syntax:

/bookstore/book[position()<3]

Selects the first two book elements that are children of the bookstore element

Upvotes: 2

Related Questions