JXPath is a java library for Object Graph Navigation using the XPath syntax.
This requires the libraries commons-jxpath-1.3.jar, commons-beanutils.jar and commons-logging.jar to be in classpath.
The following example shows using last function in JXPath.
//Get the elements relative to the last element
System.out.println(context.getValue("/stringList[last()]"));
System.out.println(context.getValue("/stringList[last()-1]"));
System.out.println(context.getValue("/stringList[last()-2]"));
//Get the elements relative to the last element
System.out.println(context.getValue("/stringArray[last()]"));
System.out.println(context.getValue("/stringArray[last()-1]"));
System.out.println(context.getValue("/stringArray[last()-2]"));
}
}
It gives the following output,
[Maths, Physics, Chemistry, Hindi, English]
English
Hindi
Chemistry
[Maths, Physics, Chemistry, Hindi, English]
English
Hindi
Chemistry