I like to use XMLBeam for such tasks:
public class Answer {
@XBDocURL("resource://data.xml")
public interface DataProjection {
public interface Topic {
@XBRead("./@id")
int getID();
@XBRead("./@percentage")
String getPercentage();
}
@XBRead("/modelingOutput/listOfDocs//document/topic")
List getTopics();
}
public static void main(final String[] args) throws IOException {
final DataProjection dataProjection = new XBProjector().io().fromURLAnnotation(DataProjection.class);
for (Topic topic : dataProjection.getTopics()) {
System.out.println(topic.getID() + ": " + topic.getPercentage());
}
}
}
There is even a convenient way to convert the percentage to float
or double
. Tell me if you like to have an example.