Ranga Ranga - 1 year ago 172
Java Question

concat() function XPath Evaluation fails while using with AutoPilot in VTD-XML

Using the VTD-XML 2.11 (Java) API, when evaluating the XPath expression concat() on the Simple text or xml elments, instead getting a result of 2.0, it fails with the following exception:

Exception in thread "main" com.ximpleware.XPathEvalException: Function Expr can't eval to node set
at com.ximpleware.FuncExpr.evalNodeSet(FuncExpr.java:1033)
at com.ximpleware.AutoPilot.evalXPath(AutoPilot.java:876)

Here is the following program:

private static String getElementValue() throws XPathParseException, XPathEvalException, NavException {
String value = null;

VTDGen gen = new VTDGen();

VTDNav nav = gen.getNav();

AutoPilot pilot = new AutoPilot(nav);
pilot.selectXPath("concat(\"Hello\", \"Mr Buddy\")");

int bufferIndex = NO_MATCH;
if((bufferIndex = pilot.evalXPath()) != NO_MATCH) {
value= nav.getXPathStringVal();
} // end of getElementValue()

Answer Source

This is my code and it seems to work fine for me... look at it and let me know your thoughts..

import com.ximpleware.*;
public class concatTest{
    public static void main(String s1[]) throws Exception {
        VTDGen vg= new VTDGen();
        String s = "<users><user><firstName>some </firstName><lastName> one</lastName></user></users>";
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot();
        ap.selectXPath("concat('good', ' luck')");
        System.out.println(" concat output ==>"+ap.evalXPathToString());
        ap.selectXPath("concat(/, '')");
        System.out.println(" concat output ==>"+ap.evalXPathToString());