需要導入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);
}
}
}