use of cn.wanghaomiao.xpath.core.XpathProcessor in project JsoupXpath by zhegexiaohuozi.
the class JXDocument method selN.
public List<JXNode> selN(String xpath) throws XpathSyntaxErrorException {
List<JXNode> finalRes = new LinkedList<>();
try {
CharStream input = CharStreams.fromString(xpath);
XpathLexer lexer = new XpathLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
XpathParser parser = new XpathParser(tokens);
parser.setErrorHandler(new DoFailOnErrorHandler());
ParseTree tree = parser.main();
XpathProcessor processor = new XpathProcessor(elements);
XValue calRes = processor.visit(tree);
if (calRes.isElements()) {
for (Element el : calRes.asElements()) {
finalRes.add(JXNode.e(el));
}
} else if (calRes.isList()) {
for (String str : calRes.asList()) {
finalRes.add(JXNode.t(str));
}
}
} catch (Exception e) {
String msg = "Please check the syntax of your xpath expr, ";
throw new XpathSyntaxErrorException(msg + ExceptionUtils.getRootCauseMessage(e), e);
}
return finalRes;
}
use of cn.wanghaomiao.xpath.core.XpathProcessor in project JsoupXpath by zhegexiaohuozi.
the class ExprTest method exp.
@Test
public void exp() {
// CharStream input = CharStreams.fromString("//ul[@class='subject-list']/li[./div/div/span[@class='pl']/num()>10000]/div[@class='info']/h2/allText()");
// CharStream input = CharStreams.fromString("//ul[@class='subject-list']/li[contains(self::li/div/div/span[@class='pl']//text(),'14582')]/div/h2//text()");
CharStream input = CharStreams.fromString("//ul[@class='subject-list']/li[contains(./div/div/span[@class='pl']//text(),'14582')]/div/h2//text()");
// CharStream input = CharStreams.fromString("//*[@id=\"subject_list\"]/ul/li[2]/div[2]/h2/a//text()");
// CharStream input = CharStreams.fromString("//*[contains(@id,\"subject_\")]/ul/li[2]/div[2]/h2/a//text()");
// CharStream input = CharStreams.fromString("//*[contains(@id,\"subject_\")]/ul/li[2]/div[2]/h2/a//text()");
XpathLexer lexer = new XpathLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
XpathParser parser = new XpathParser(tokens);
parser.setErrorHandler(new DoFailOnErrorHandler());
ParseTree tree = parser.main();
XpathProcessor processor = new XpathProcessor(root);
XValue value = processor.visit(tree);
logger.info("visit res = {}", value);
}
Aggregations