Ranga Ranga - 7 months ago 107
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();
gen.setDoc(data.getBytes());
gen.parse(false);

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();
}
System.out.println(value);
} // end of getElementValue()

Answer

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>";
        vg.setDoc(s.getBytes());
        vg.parse(false);
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot();
        ap.selectXPath("concat('good', ' luck')");
        System.out.println(" concat output ==>"+ap.evalXPathToString());
        ap.selectXPath("concat(/, '')");
        ap.bind(vn);
        System.out.println(" concat output ==>"+ap.evalXPathToString());
    }

}
Comments