当前位置:网站首页>2021.8.6 notes jsoup
2021.8.6 notes jsoup
2022-07-27 18:33:00 【It's food, not shellfish】
jsoup object


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( file ( Need to path), Encoding mode ) */
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 object
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 object
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);
}
}
Query by selector
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"); //> If yes, it is a level-1 sub tag
System.out.println(newEle2);
}
}
边栏推荐
- [MIT 6.S081] Lab 7: Multithreading
- 技术分享| 快对讲综合调度系统
- Deep learning: installation package records
- Read only mode of buffer
- @Considerations for query of convert annotation in JPA
- 动态链表3队列的链式存储结构(LinkedQueue实现)
- [MIT 6.S081] Lec 9: Interrupts 笔记
- Deep learning: GCN (graph convolution neural network) theory learning summary
- 发布自己的npm组件库
- Deep learning: stgcn learning notes
猜你喜欢

数据库的常用命令1

Deep learning: Gan case exercise -minst handwritten digits

MySQL学习 Day3 多表查询 / 事务 / DCL

2021.7.19笔记 DML

How do corporate giants such as Schneider Electric and L'Oreal make open innovation? Uncover secrets of demo World Innovation Summit

2021.7.30笔记 索引
![[MIT 6.S081] Lab 6: Copy-on-Write Fork for xv6](/img/ca/e8c0827b13805c7c74cc41bf84c6ff.png)
[MIT 6.S081] Lab 6: Copy-on-Write Fork for xv6

Error launching IDEA
![[mit 6.s081] LEC 4: page tables notes](/img/30/f1e12d669b656a0b14aa8c7b210085.png)
[mit 6.s081] LEC 4: page tables notes

2021.8.9笔记 request
随机推荐
微信小程序 wxacode.getUnlimited生成小程序码
"Who is Huawei" documentary film series landing on BBC: exposing a large number of Ren Zhengfei's unknown experience
Lotcode dynamic array exercise (724118766)
2021.7.19 notes DML
An analysis of CPU explosion of a smart logistics WCS system in.Net
Disassembly of Xiaomi cc9 Pro: the cost of rear five shots is several times that of Xiaolong 855!
2021.7.31笔记 视图
华为Mate30 Pro 5G拆解:自研芯片占比过半,美系芯片依然存在!
[MIT 6.S081] Lab 5: xv6 lazy page allocation
Deep learning: Gan optimization method dcgan case
Announcing the acquisition of 30% shares of Wenye, what is the general intention of Dalian United?
XML学习 Day1 : xml / Jsoup解析器 / selector选择器 /Xpath选择器
Deep recognition: thesis reading_ 2s-agcn cvpr2019 (two stream adaptive graph convolution network based on skeleton action recognition)
Software installation related
The end of another era!
Navicat 导出表生成PDM文件
[MIT 6.S081] Lab 3: page tables
[MIT 6.S081] Lab 6: Copy-on-Write Fork for xv6
[MIT 6.S081] Lab 4: traps
[mit 6.s081] LEC 5: calling conventions and stack frames risc-v notes