当前位置:网站首页>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] Lec 3: OS organization and system calls 笔记
- 请教大神一个问题 flinkcdc,同步mysql中的datetime字段会变为时间戳 有人遇到过吗
- How do corporate giants such as Schneider Electric and L'Oreal make open innovation? Uncover secrets of demo World Innovation Summit
- Deep learning: GCN diagram classification case
- @Datetimeformat received less than minutes and seconds, conversion times type exception
- 数据库的常用命令1
- 二叉树概念
- [MIT 6.S081] Lab 7: Multithreading
- 2021.7.30笔记 索引
- 2021.8.1笔记 数据库设计
猜你喜欢

深度学习-视频行为识别:论文阅读——双流网络(Two-stream convolutional networks for action recognition in videos)
![[mit 6.s081] LEC 8: page faults notes](/img/e2/0f5332dd9d2b439bcf29e87a9fa27f.png)
[mit 6.s081] LEC 8: page faults notes

1. OpenCV image basic operation

Meituan Er Mian: why does redis have sentinels?

View port PID and end process

File path read

1. opencv图片基础操作

MySQL学习 Day2 排序查询 / 聚合函数 /分组查询 /分页查询 /约束/多表之间的关系

3. Opencv geometric transformation

深度学习:GAN案例练习-minst手写数字
随机推荐
Jrs-303 usage
微信小程序获取openId, sessionKey, unionId
Deep learning: GCN case
[MIT 6.S081] Lab 7: Multithreading
Zhanrui fresh seedlings: enable full scene applications, and massive data needs the integration of AI and IOT
微信小程序 wxacode.getUnlimited生成小程序码
2021.7.30笔记 索引
Technology sharing | quick intercom integrated dispatching system
深度学习:STGCN学习笔记
NowCoder(5选)
3. opencv几何变换
org.apache.catalina.core.StandardContext. startInternal Context [] startup failed due to previous err
请教大神一个问题 flinkcdc,同步mysql中的datetime字段会变为时间戳 有人遇到过吗
Error launching IDEA
2021.8.9笔记 request
uniapp H5跨域问题
[MIT 6.S081] Lab 6: Copy-on-Write Fork for xv6
[MIT 6.S081] Lec 1: Introduction and examples 笔记
深度学习-论文阅读:动作结构性图卷积网络AS-GCN
After being "expelled" from bitland, the Jank group said for the first time: it will return as soon as possible through legal channels!