使用VTD-XML 2.11(Java)API,在Simple文本或xml元素上评估XPath表达式 concat(),而不是得到2.0的结果时,它会失败并出现以下异常:
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)
以下是以下计划:
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()
答案 0 :(得分:1)
这是我的代码,它似乎对我很好......看看它,让我知道你的想法..
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());
}
}