Jsoup中Xpath解析器

需要导入xpath的包

import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import cn.wanghaomiao.xpath.model.JXNode;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.IOException;
import java.util.List;

//xpath解析
public class JsoupDemo6 {
    public static void main(String[] args) throws IOException, XpathSyntaxErrorException {
        String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath();
        Document document = Jsoup.parse(new File(path),"utf-8");
        JXDocument jxDocument = new JXDocument(document);
        //获取dom树种的student标签的内容
        List<JXNode> jxNodes = jxDocument.selN("//student");
        for (JXNode jxNode : jxNodes) {
            System.out.println(jxNode);
        }
        System.out.println("-----------------");
        //获取number属性的值  @获取属性值
        List<JXNode> nodes = jxDocument.selN("//@number");
        for (JXNode node : nodes) {
            System.out.println(node);
        }
    }
}


posted @ 2021-05-05 22:22  code-G  阅读(1120)  评论(0编辑  收藏  举报