当前位置:网站首页>2021.8.6笔记 jsoup
2021.8.6笔记 jsoup
2022-07-27 16:10:00 【是吃货不是贝爷】
jsoup对象


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class xmlJsoupPra {
public static void main(String[] args) throws IOException {
/** * 1.parse(文件(需要传入path), 编码方式) */
String path = xmlJsoupPra.class.getClassLoader().getResource("student.xml").getPath();
Document document = Jsoup.parse(new File(path), "utf-8");
Elements eles = document.getElementsByTag("name");
Element element = eles.get(0);
String text = element.text();
System.out.println(text);
/** * 2.String */
String str = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
"<students>\n" +
" <studnt number=\"1001\">\n" +
" <name>zhangsan</name>\n" +
" <age>18</age>\n" +
" <grender>male</grender>\n" +
" </studnt>\n" +
" <studnt number=\"1002\">\n" +
" <name>lisi</name>\n" +
" <age>15</age>\n" +
" <grender>female</grender>\n" +
" </studnt>\n" +
"</students>";
Document parse1 = Jsoup.parse(str);
System.out.println(parse1);
/** * 3.url */
URL url = new URL("https://www.baidu.com");
Document parse2 = Jsoup.parse(url, 10000);
System.out.println(parse2);
}
}
Document对象
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
public class DocumentObj {
public static void main(String[] args) throws IOException {
String path = DocumentObj.class.getClassLoader().getResource("student.xml").getPath();
Document docu1 = Jsoup.parse(new File(path), "utf-8");
Elements ele1 = docu1.getElementsByAttribute("number");
Element element = ele1.get(0);
System.out.println(element);
Elements ele2 = docu1.getElementsByAttributeValue("number", "1001");
System.out.println(ele2);
Element ele3 = docu1.getElementById("i5");
System.out.println(ele3);
}
}
Element对象
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
public class ElementObj {
public static void main(String[] args) throws IOException {
String path = ElementObj.class.getClassLoader().getResource("student.xml").getPath();
Document docu1 = Jsoup.parse(new File(path), "utf-8");
Element ele1 = docu1.getElementsByTag("student").get(0);
Elements ele2 = ele1.getElementsByTag("name");
System.out.println(ele2);
String num = ele1.attr("number");
System.out.println(num);
Element age = ele1.getElementsByTag("age").get(0);
String text = age.text();
System.out.println(text);
String htmlAge = age.html();
System.out.println(htmlAge);
}
}
根据选择器查询
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
public class SelectObj {
public static void main(String[] args) throws IOException {
String path = SelectObj.class.getClassLoader().getResource("student.xml").getPath();
Document docu = Jsoup.parse(new File(path), "utf-8");
Elements nameEle = docu.select("name");
System.out.println(nameEle);
Elements IdEle= docu.select("#i5");
System.out.println(IdEle);
Elements newEle = docu.select("name[id=\"i5\"]");
System.out.println(newEle);
Elements newEle2 = docu.select("student[number=\"1001\"] age"); //>的话是一级子标签
System.out.println(newEle2);
}
}
边栏推荐
- Es query limit 10000 data solutions
- 浅论分布式训练中的recompute机制
- 【学习笔记】Redis中有序集合zset的实现原理——跳表
- 浅谈AI深度学习的模型训练和推理
- 深度学习:行为识别综述
- Deep learning: a survey of behavior recognition
- 深度学习:GCN案例
- Announcing the acquisition of 30% shares of Wenye, what is the general intention of Dalian United?
- 图形界面编程
- 英特尔发布新一代Movidius VPU:性能提升10倍,功耗仅30W
猜你喜欢

Super practical! After reading the kubernetes study notes hidden by Alibaba P9, call NB directly

Deep learning - VIDEO behavior recognition: paper reading - two stream revolutionary networks for action recognition in videos

Dynamic linked list 4 one-way circular linked list (loopsingle Implementation)

Three consecutive high-frequency interview questions of redis online celebrity: cache penetration? Cache breakdown? Cache avalanche?

Technology sharing | quick intercom integrated dispatching system
![[MIT 6.S081] Lab 9: file system](/img/f5/ea30b1fe5b6d73c86f2509c690ca20.png)
[MIT 6.S081] Lab 9: file system

2. Change color space and color detection

技术分享| 快对讲综合调度系统

查看端口PID及结束进程

Chained storage structure of dynamic linked list 3 queue (linkedqueue Implementation)
随机推荐
Jianan Yunzhi has completed the pre roadshow and is expected to land on NASDAQ on November 20
超实用!阿里P9私藏的Kubernetes学习笔记,看完直呼NB
Salesforce Dynamic Forms
深度学习:GAN优化方法-DCGAN案例
[learning notes] solutions to hot account problems
力压谷歌、英伟达!阿里含光800芯片再获权威测试世界第一
动态链表3队列的链式存储结构(LinkedQueue实现)
Golang Chan implements mutual exclusion
Deep learning: installation package records
After being "expelled" from bitland, the Jank group said for the first time: it will return as soon as possible through legal channels!
英特尔发布新一代Movidius VPU:性能提升10倍,功耗仅30W
Mysql四种锁
【学习笔记】热点账户问题的解决方案
浅谈AI深度学习的模型训练和推理
Golang customize once. When error occurs, reset it for the second time
文件路径读取
Huawei mate30 Pro 5g disassembly: self developed chips account for more than half, and American chips still exist!
浅论分布式训练中的recompute机制
Marvell公布旗下Arm服务器芯片路线图,下一代性能将比ThunderX2高两倍
深度学习:GAT