当前位置:网站首页>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);
}
}
边栏推荐
- [MIT 6.S081] Lab 4: traps
- 被“赶出”比特大陆之后,詹克团首度发声:将通过法律途径尽快回归!
- [MIT 6.S081] Lec 4: Page tables 笔记
- 收藏!0 基础开源数据可视化平台 FlyFish 大屏开发指南
- [MIT 6.S081] Lab 9: file system
- 又一个时代的终结!
- Press Google and NVIDIA! Alibaba optical 800 chip won the world's first authoritative test again
- Deep learning: Gan case exercise -minst handwritten digits
- Is it difficult to operate email safely? COREMAIL joins hands with cloud store to create a new ecosystem of corporate email office!
- 深度学习:GAN案例练习-minst手写数字
猜你喜欢
![[learning notes] the implementation principle of the ordered set Zset in redis - skip table](/img/c6/5d9f48fce1dc1c78b8c7dbcf046fc7.png)
[learning notes] the implementation principle of the ordered set Zset in redis - skip table

深度学习:GAN案例练习-minst手写数字

解决Reids不能被其他IP访问

二叉树概念
![[MIT 6.S081] Lec 9: Interrupts 笔记](/img/b6/a8d39aa7ede4eb1c5a74e6d15b3b1c.png)
[MIT 6.S081] Lec 9: Interrupts 笔记
![[MIT 6.S081] Lab 5: xv6 lazy page allocation](/img/f6/8b619412bc6ba425d0f04629917e80.png)
[MIT 6.S081] Lab 5: xv6 lazy page allocation

Deep learning: Gan case exercise -minst handwritten digits

Deep learning - paper reading: action structural graph convolution network as-gcn

LootCode动态数组练习(724,118,766)

Publish your own NPM component library
随机推荐
Deep learning: GCN case
图形界面编程
[MIT 6.S081] Lec 5: Calling conventions and stack frames RISC-V 笔记
荣耀、小米发双十一战报:都称自己是冠军
[MIT 6.S081] Lab 3: page tables
Huawei mate30 Pro 5g disassembly: self developed chips account for more than half, and American chips still exist!
Bug records using distributed framework WCF
【学习笔记】数据库中锁的分类
Multi thread implementation loop
Intel releases a new generation of movidius Vpu: 10 times higher performance and 30W power consumption
Redis网红高频面试题三连:缓存穿透?缓存击穿?缓存雪崩?
携手三星,vivo将推Exynos980双模5G手机!
Deep learning: Gan optimization method dcgan case
国巨斥资18亿美元收购竞争对手Kemet,交易或在明年下半年完成
Salesforce runs all test classes and gets coverage reports
浅谈AI深度学习的模型训练和推理
[MIT 6.S081] Lab 7: Multithreading
Common commands of database 1
[MIT 6.S081] Lec 6: Isolation & system call entry/exit 笔记
You can't specify target table 'table name' for update in from clause error resolution in MySQL