当前位置:网站首页>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);
}
}
边栏推荐
- Huawei mate30 Pro 5g disassembly: self developed chips account for more than half, and American chips still exist!
- 数据库的常用命令1
- 【学习笔记】Redis中有序集合zset的实现原理——跳表
- Intel releases a new generation of movidius Vpu: 10 times higher performance and 30W power consumption
- @DateTimeFormat 接收不到时分秒,转换时报类型异常
- Multi thread import data and generate error files for redis storage
- Getting started with typora: the most complete tutorial in the whole network
- [learning notes] Lombok's @builder annotation
- [learning notes] the implementation principle of the ordered set Zset in redis - skip table
- 江苏华存首发PCIe 5.0 SSD主控:台积电12nm工艺,2020年量产
猜你喜欢

Deep learning: Gan optimization method dcgan case

深度学习-论文阅读:动作结构性图卷积网络AS-GCN
![[MIT 6.S081] Lab 10: mmap](/img/5d/a59a6f723518553b9232bc09991075.png)
[MIT 6.S081] Lab 10: mmap

深度学习:GAN优化方法-DCGAN案例
![[learning notes] advanced version of MySQL database - index optimization, slow query, locking mechanism, etc](/img/7a/7497a73b435c3ed4fa0f3a3e908298.jpg)
[learning notes] advanced version of MySQL database - index optimization, slow query, locking mechanism, etc

View port PID and end process
![[MIT 6.S081] Lab 9: file system](/img/f5/ea30b1fe5b6d73c86f2509c690ca20.png)
[MIT 6.S081] Lab 9: file system

【学习笔记】MySQL数据库高级版 - 索引优化、慢查询、锁机制等

Deep learning: Gan case exercise -minst handwritten digits
![[MIT 6.S081] Lab 5: xv6 lazy page allocation](/img/f6/8b619412bc6ba425d0f04629917e80.png)
[MIT 6.S081] Lab 5: xv6 lazy page allocation
随机推荐
Chained storage structure of dynamic linked list 3 queue (linkedqueue Implementation)
深度学习-论文阅读:动作结构性图卷积网络AS-GCN
深度学习:GCN图分类案例
[MIT 6.S081] Lab 11: networking
深度学习:GCN案例
@DateTimeFormat 接收不到时分秒,转换时报类型异常
《华为是谁》纪录短片集登陆BBC:曝光大量任正非不为人知经历
展锐鲜苗:赋能全场景应用,海量数据需要AI与IoT融合
[MIT 6.S081] Lec 5: Calling conventions and stack frames RISC-V 笔记
Multi thread implementation loop
@Scheduled and quartz
图形界面编程
After being "expelled" from bitland, the Jank group said for the first time: it will return as soon as possible through legal channels!
深度学习:GAT
Local development using LWC in salesforce
Intel releases a new generation of movidius Vpu: 10 times higher performance and 30W power consumption
技术分享| 快对讲综合调度系统
Hackers use lasers to attack, and voice assistants can be activated from a distance of 100 meters
Mysql四种锁
Please ask the great God a question, flinkcdc, the datetime field in synchronous MySQL will become a timestamp. Has anyone ever encountered it