使用 VTD-XML 2.11 (Java) API,在简单文本或 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()
最佳答案
这是我的代码,它似乎对我来说工作得很好...看看它,让我知道你的想法..
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());
}
}
关于java - concat() 函数在 VTD-XML 中与 AutoPilot 一起使用时 XPath 评估失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35307154/