使用Jsoup处理本地文件

使用Jsoup处理本地文件

1、处理本地文件时先使用File类打开文件

在这里插入图片描述

File file = new File("D:\\a.xml");
Document document = Jsoup.parse(file, "utf-8");
/*
*获取rows标签中的所有内容(getElementsByTag获取标签内容)
*无论有一个或多个同级rows都将将存放到数组中
*getElementsByTag("row")是获取rows中的子标签,同样是一个数组
*/
Elements rows = document.getElementsByTag("rows") .get(0).getElementsByTag("row");
for (Element row : rows) {
    
    
		//获取row的id值   ——》id
		row.id();
		/*
		*获取row中的cell标签,然后将它遍历,也可以使用for(Element str : cell)
		*/
		Elements cell = row.getElementsByTag("cell");
        for (int i = 0; i < cell.size(); i++) {
    
    
        	Element str = cell.get(i);
        	//获取标签中的内容 张三  李四 王五 赵六
        	String text = str.text()//
        }
}

2、其他常用选择器

	getElementById(String id):通过id来获取
	getElementsByClass(String className):通过类名来获取
	getElementsByAttribute(String key):通过属性名字来获取
	getElementsByAttributeValue(String key, String value):通过指定的属性名字,属性值来获取
	getAllElements():获取所有元素
	select();参数:id class 标签等
	......

3、获取标签的id,className等的值

className();
cssSelector();
tagName();
tag();
......

猜你喜欢

转载自blog.csdn.net/G_liunian/article/details/103617440