xml+html 概述
1.什么是xml
xml 是可扩展标记语言的缩写: Extensible Markup Language。
<root><h1> text 1</h1> </root>
web 应用开发,需要配置 web.xml,就是个典型的 xml文件
<web-app><servlet><servlet-name>HelloServlet</servlet-name><servlet-class>HelloServlet</servlet-class></servlet><servlet-mapping><servlet-name>HelloServlet</servlet-name><url-pattern>/hello</url-pattern></servlet-mapping></web-app>
dao或者mapper层映射文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.xml.test.mapper.helloMapper"></mapper>
上述的 root、h1、 web-app、servlet-name....mapper 整体称为 元素
简单举例: <h1> text 1</h1> 格式为 :<元素名称> 元素内容 </元素名称>
h1就是元素名称, text 1 是元素内容。整体就是一个元素。
2.什么是html
html 是 HyperText Markup Language的缩写,超文本标记语言。
<html><body><p>Hello HTML</p></body> </html>
是不是发现 类似 xml?
html 可以简单看成是 xml 的一个子集。
html 用的都是一些预先定义的元素,如 <html>, <a>, <body>, <table> 。
xml 什么元素都可以自定义: 如 <a> , <b>, <aabb> 。
3.XML 和 HTML 的区别:
-
目的:
- XML:用于存储和传输数据,强调数据的结构化。
- HTML:用于创建和显示网页内容,强调如何展示数据。
-
自定义标签:
- XML:可以自定义标签。
- HTML:使用预定义的标签。
-
语法要求:
- XML:要求严格的语法,每个开始标签必须有对应的结束标签,并且标签大小写敏感。
- HTML:语法相对宽松,标签大小写不敏感,部分标签可以没有结束标签(例如
<li>
)。
-
使用场景:
- XML:主要用于数据交换,如 Web 服务中的数据传输,配置文件,文档存储等。
- HTML:用于创建网站页面,显示内容给用户。 为啥叫超文本标记语言?因为超越一般文本,其他文本记录数据,而html 记录数据同时,还能呈现出文字颜色,位置,大小等。
4.解析 xml
解析的几种方式:
1. java 本身自带对 xml 的解析。在 javax.xml 这个包下,非常难用,难用到想吐。 这种方式叫做 sax/dom
2. 因为 java 自带的很难用,所以就出现了更方便的第三方工具 dom4j, 解析效率大大提高了。 XML从入门到深入(超详细)-CSDN博客 推荐文章
3. 现在又出现了更方便的 jsoup, 我们就会讲解如何用 jsoup 来解析 xml。前面讲过html 看作xml子集,解析方式一样的。
要求解析:<html><body><p>Hello HTML</p></body></html>
任何开发,除了jdk,jvm自带的包,都要先下载jar包
如:jsoup-1.11.2.jar
package com.test.xml.analysis.example;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;public class Test {public static void main(String[] args) throws Exception {String html = "<html><body><p>Hello HTML</p></body></html>";
//1. 把文本解析成 Document 对象, Document 对象就代表整个 xml 文档。Document doc = Jsoup.parse(html);//2. 获取所有的 p 元素Elements as= doc.getElementsByTag("p");for (Element e : as) {
//3.打印出p元素中的元素内容System.out.println(e.text());}}
}
5.获取文档 针对4的拆分
5.1获取文档
获取Document对象的方式有多种,常见的就是基于 字符串,文件,网页地址。
package com.test.xml.analysis.example;import java.io.File; import java.net.URL;import org.jsoup.Jsoup; import org.jsoup.nodes.Document;public class Test {public static void main(String[] args) throws Exception {String html1 = "<html><body><p>Hello HTML</p></body></html>";Document doc = Jsoup.parse(html1);System.out.println("基于字符串方式得到的 Document:\r\n"+ doc);File f = new File("a.html");if(f.exists()) {Document fileDoc = Jsoup.parse(f,"utf-8");System.out.println("基于文件方式得到的 Document:\r\n"+ fileDoc);}String url = "http://www.baidu.com";Document urlDoc= Jsoup.parse(new URL(url),5000); //超过5秒就报错System.out.println("基于URL方式得到的 Document:\r\n"+ urlDoc);} }
5.2 获取元素
获取元素比较常见的几种方式: 通过id, 标签或者类名称获取。
假设有文件 b.html 里面内容为:
<div class="header-container"><div class="bookwrapper clearfix"> <div id="brandProdName"><div id="logocover"></div><div id="productName" >Documentation</div></div><br class="clearfloat" /></div></div><a href="https://blog.csdn.net/weixin_48306950/article/details/121639656">循环依赖解决</a> <a href="https://www.baidu.com/">百度网址 </a> <form id="searchForm" onsubmit="return search()"><input type="hidden" name="category" value="java"/><input type="hidden" name="product" value="e25407-01"/><input type="hidden" name="q" value="" id="keywordreal"/><label for="searchField"><input type="text" id="searchField" value="Search the online Java Tutorials"size="30" onclick="this.value='';" /></label><input type="submit" value="Submit" /> </form>
java对应 获取元素代码
public static void main(String[] args) throws Exception {File f = new File("b.html");if(!f.exists())return;Document doc = Jsoup.parse(f,"utf-8");//通过id获取 <div id="productName" >Documentation</div>Element e = doc.getElementById("productName");System.out.println(e);//通过标签获取 <a href="h....Elements es;es = doc.getElementsByTag("a");show(es);//通过类名称获取 <div class="header-container">es = doc.getElementsByClass("header-container");show(es);//通过属性获取 <input type="hidden" name="category" value="java"/>es = doc.getElementsByAttribute("name");show(es);}private static void show(Elements es) {for (Element e : es) {System.out.println(e);}}
5.3获取文本和内容
区分文本 内容 <a><b>123</b></a>
- 内容 是指节点的所有数据,包括子元素。 元素a之间为内容
- 文本 是指元素内的纯文本数据,不包括任何标签。 元素b之间为内容
c.html 如下
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <div id="d1" class="class1test class2" ><p>Hello, world!</p> </div></body> </html>
public static void main(String[] args) { /* File f = new File("c.html");if (!f.exists()) return;*/// 通过类加载器获取资源文件输入流InputStream ins = ResourceLoader.class.getClassLoader().getResourceAsStream("c.html");Document doc = Jsoup.parse(ins, "utf-8", "");Element e = doc.getElementById("d1");//获取属性System.out.println(e.attr("id")); //d1//获取所有属性System.out.println(e.attributes()); // id="d1" class="class1test class2"//获取idSystem.out.println(e.id()); //d1//获取类名称System.out.println(e.className());// class1test class2//获取所有类名称System.out.println(e.classNames());// [class1test, class2]//获取文本System.out.println(e.text()); // Hello, world!//获取html/内容System.out.println(e.html()); // <p>Hello, world!</p>//获取外html 本质就是该元素本身System.out.println(e.outerHtml()); /* <div id="d1" class="class1test class2"> <p>Hello, world!</p> </div> *///获取标签信息System.out.println(e.tagName()); // div }
注意:在 Maven 项目中,如果
c.html
文件放在src/main/resources
目录下,那么该文件会被打包到target/classes
目录里,作为类路径资源进行访问。你不能直接使用new File("c.html")
来访问这个文件这样在当前工作目录中查找该文件,而不是在类路径中。
回忆下之前概念。 对比上述 div标签 中 添加的内容带 p 标签 ,而不仅是只有文本 Hello
5.4 选择器
选择器语法
在选择元素的时候,除了使用方法名如 getElementById 这样的外,还可以用 选择器语法来选择。 操作起来就像 jquery,比如getElementById 就可以写成是 select("#id")。
package com.test.xml.example;import java.io.File;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;public class Test {static Document doc;public static void main(String[] args) throws Exception {// 通过类加载器获取资源文件输入流InputStream ins = ResourceLoader.class.getClassLoader().getResourceAsStream("b.html");doc = Jsoup.parse(ins,"utf-8","");//像 jquery 那样的选择器语法show("选择所有的超链", "a");show("根据id进行选择", "#logocover");show("根据class进行选择", ".clearfloat");show("根据属性进行选择", "[href]");show("有属性以tar开头", "[^tar]");show("根据属性值选择", "[type='application/javascript']");show("属性值以什么开头", "[href^='http://www.oracle.com']");show("属性值以什么结尾", "[href$='index.html']");show("属性值包含什么", "[href*='download']");}private static void show(String text, String selector) {show(text,selector,3);}private static void show(String text, String selector, int limit) {Elements es =doc.select(selector);if(es.size()>1)System.out.println(String.format("%s - 使用的选择器是: \"%s\" \t (最多显示 %d 条 )", text,selector,limit)); elseSystem.out.println(String.format("%s - 使用的选择器是: \"%s\"", text,selector)); int i =0;for (Element e : es) {if(i++<limit)System.out.println(e);}System.out.println();}}
b.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xmlns="http://www.w3.org/1999/xhtml" xml:lang=
"en-US">
<head>
<title>The Java™ Tutorials</title>
<meta name="Description" content=
"Tutorials and reference guides for the Java Programming Language" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<style type="text/css">
/*<![CDATA[*/body {margin-left:10px;margin-right:10px;line-height: 1.5;FONT-FAMILY: Arial, Helvetica, sans-serif; font-size: 0.8em;}a:link{text-decoration:none; color:#09569d;}a:visited{text-decoration:none; color: #3a87cf;}a:hover{text-decoration:underline; }.header-container {background-color: #fff;border-bottom: 1px solid #C1CFDA;-webkit-box-shadow: 0 2px 2px rgba(117, 163, 231, 0.1);box-shadow: 0 2px 2px rgba(117, 163, 231, 0.1);}.bookwrapper {width: auto;margin: auto;}.clearfix {}.clearfloat {clear: both;overflow: auto;height: 0px;font-size: 1px;line-height: 0px;}#brandProdName {width: auto;height: auto;}#logocover {display: block;background: transparent url(images/oracle-java-logo.png) 0px 0px no-repeat;height: 50px;width: 229px;float: left;}#productName {font-size: 16px;position: relative;top: 19px;padding-left: 3px;color: #457798;white-space: nowrap;width: 340px;}#TopBar_bl { width: 100%;height: 60px;}#TopBar_br {width: 100%;height: 60px;}#TopBar_tl {margin-left: -110px;margin-right: -100px; align: left;height: 60px;}#TopBar_tr {width: 100%;height: 60px;}#TopBar {min-width:700px;padding:25px 100px 10px;margin-bottom:25px;clear:both;border-bottom:1px solid #d2dde5;border-radius: 3px;background:#efefef; /* Old browsers *//* IE9 SVG, needs conditional override of 'filter' to 'none' */background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2ZmZmZmZiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNlMmVmZjkiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);background: -moz-linear-gradient(top, #ffffff 0%, #e2eff9 100%); /* FF3.6+ */background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(100%,#e2eff9)); /* Chrome,Safari4+ */background: -webkit-linear-gradient(top, #ffffff 0%,#e2eff9 100%); /* Chrome10+,Safari5.1+ */background: -o-linear-gradient(top, #ffffff 0%,#e2eff9 100%); /* Opera 11.10+ */background: -ms-linear-gradient(top, #ffffff 0%,#e2eff9 100%); /* IE10+ */background: linear-gradient(to bottom, #ffffff 0%,#e2eff9 100%); /* W3C */filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#e2eff9',GradientType=0 ); /* IE6-8 */}#TopBar_left {line-height: 14px;position: absolute;padding-top: 30px;padding-right: 30px;padding-left: 30px;text-align: left;font: 13px/20px Arial, Helvetica, sans-serif;font-weight: bold;font-size: 20px;color: #333;}@media print {div#TopBar_left {margin-left: 0;}}@media print {div#TopBar_right {display: none;}}#TopBar_right {line-height: 12px;float: right;padding-top: 10px;padding-right: 30px;text-align: left;}#TopBar_right a {font-size: 12px;margin: 3px;padding: 0;}#Footer {padding-top: 10px;padding-left: 10px;margin-right: 10px; }.footertext {font-size: 10px;font-family: sans-serif; margin-top: 1px;}div#TutBody {margin: 10px 20em 10px 3em;}div.RightBar {font-family: sans-serif; float: right;}div.RightBar img {margin: 0 0 1em 0;}div.RightBox {margin: 10px 3em 10px 0;width: 15em;border-style: double;}div.BlueRightBox {margin: 10px 3em 10px 0;width: 15em;border-style: double;background:#efefef; /* Old browsers *//* IE9 SVG, needs conditional override of 'filter' to 'none' */background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2ZmZmZmZiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNlMmVmZjkiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);background: -moz-linear-gradient(top, #ffffff 0%, #e2eff9 100%); /* FF3.6+ */background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(100%,#e2eff9)); /* Chrome,Safari4+ */background: -webkit-linear-gradient(top, #ffffff 0%,#e2eff9 100%); /* Chrome10+,Safari5.1+ */background: -o-linear-gradient(top, #ffffff 0%,#e2eff9 100%); /* Opera 11.10+ */background: -ms-linear-gradient(top, #ffffff 0%,#e2eff9 100%); /* IE10+ */background: linear-gradient(to bottom, #ffffff 0%,#e2eff9 100%); /* W3C */filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#e2eff9',GradientType=0 ); /* IE6-8 */}div.Pad {font-size: 80%;padding: 1em;}div.Pad h2 {text-align: center;margin: 0;}div.Pad div {display: list-item;list-style-image: url(images/arrow-right-gray.gif);margin: 0.3em 1em;}ul.BlueArrows {list-style-image: url(images/ar_dbl_blue.gif);}ul.BlueArrows a:visited, ul.BlueArrows a:link {text-decoration: none;}ul.BlueArrows a:hover, ul.BlueArrows a:active {text-decoration: underline;}h1, h2, h3, h4, h5 {color: #333;font-family: sans-serif;}h1 {font-weight: bold;font-size: 20px;}h2 {font-weight: bold;font-size: 17px;}h3 {font-weight: bold;font-size: 14px;}h4 {font-size: 15px;}h5 {font-size: 12px;}/*]]>*/
</style>
<script>window.ohcglobal || document.write('<script src="https://how2j.cn/en/dcommon/js/global.js">\x3C/script>')</script><script src='/en/dcommon/js/disclaimer.js' defer></script></head>
<body>
<noscript>A browser with JavaScript enabled is required for this
page to operate properly.</noscript>
<div class="header-container"><div class="bookwrapper clearfix"> <div id="brandProdName"><div id="logocover"></div><div id="productName" >Documentation</div></div> <br class="clearfloat" /></div>
</div><div id="TopBar"><div id="TopBar_tr"><div id="TopBar_tl"><div id="TopBar_br"><div id="TopBar_bl"> <div id="TopBar_left">The Java™ Tutorials</div><div id="TopBar_right"><script type="text/javascript">
function search() {var sform = document.getElementById("searchForm");var srchelem = document.getElementById("searchField");var srchelemreal = document.getElementById("keywordreal");var srchval = srchelem.value;if (srchval.length == 0) {return false;}srchelemreal.value = srchval;sform.action = "http://docs.oracle.com/apps/search/search.jsp";sform.method = "get";sform.target = "_blank";sform.submit();
}
</script><form id="searchForm" onsubmit="return search()"><input type="hidden" name="category" value="java"/><input type="hidden" name="product" value="e25407-01"/><input type="hidden" name="q" value="" id="keywordreal"/><label for="searchField"><input type="text" id="searchField" value="Search the online Java Tutorials"size="30" onclick="this.value='';" /></label><input type="submit" value="Submit" />
</form></div> </div></div></div></div>
</div><div class="RightBar"><img src="images/ThinkingDuke.png" width=
"124" height="128" align="middle" alt=
"Duke thinking about what to study" style="margin-top:2px" /><br />
Not sure where to start?<br />
See <a href="tutorialLearningPaths.html">Learning Paths</a>
<div class="BlueRightBox">
<center>
<h2>Tutorial Contents</h2>
</center>
<div>
<center><a href="reallybigindex.html"><img src=
"images/really_big_index_button.gif" width="108" height="22" align=
"middle" alt="really big index button" /></a></center>
</div>
</div>
<div class="RightBox">
<div class="Pad">
<h2>Tutorial Resources</h2>
<div>Last Updated <a href=
"information/history.html">7/19/2016</a></div>
<div><a href="https://blogs.oracle.com/thejavatutorials/">The Java
Tutorials' Blog</a> has news and updates about the Java SE
tutorials.</div>
<div><a href=
"http://www.oracle.com/technetwork/java/javase/java-tutorial-downloads-2005894.html">
Download the latest Java Tutorials bundle</a>.</div>
</div>
</div>
<div class="RightBox">
<div class="Pad">
<h2>In Book Form</h2>
<div><i>The Java Tutorial, Sixth Edition.</i> <a href=
"http://www.amazon.com/The-Java-Tutorial-Course-Edition/dp/0134034082"
target="_blank">Amazon.com</a>.</div>
</div>
</div>
<div class="RightBox">
<div class="Pad">
<h2>Other Resources</h2>
<div><a href="https://docs.oracle.com/javase/8/docs/">Java SE 8
Developer Guides</a></div>
<div><a href="https://docs.oracle.com/javase/8/docs/api">JDK 8 API
Documentation</a></div>
</div>
</div>
<div class="RightBox">
<div class="Pad">
<h2>Oracle Training and Professional Certification</h2>
<div><a href=
"http://education.oracle.com/pls/web_prod-plq-dad/ou_product_category.getFamilyPage?p_family_id=48">
Java Certification and Training</a></div>
<div><a href=
"http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=3">
Oracle University</a></div>
</div>
</div>
<div class="RightBox">
<div class="Pad">
<h2>Software</h2>
<div>The <a href=
"http://www.oracle.com/technetwork/java/javase/downloads/index.html">
Java Development Kit</a> (JDK)</div>
<!-- <div><a href="https://netbeans.org/">NetBeans IDE</a></div> --><!--<div><a href=
"http://www.oracle.com/technetwork/java/javaee/downloads/index.html">
Java EE SDK</a></div>--></div>
</div>
</div>
<div id="TutBody">
<p style="background-color: rgb(247, 248, 249); border-width: 1px; padding: 10px; font-style: italic; border-style: solid; border-color: rgb(64, 74, 91);">The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.<br/>See <a href="https://www.oracle.com/technetwork/java/javase/jdk-relnotes-index-2162236.html">JDK Release Notes</a> for information about new features, enhancements, and removed or deprecated options for all JDK releases.</p>
<p>The Java Tutorials are practical guides for
programmers who want to use the Java programming language to create
applications. They include hundreds of complete, working examples,
and dozens of lessons. Groups of related lessons are organized into
"trails".</p><h2>Trails Covering the Basics</h2>
These trails are available in book form as <i>The Java Tutorial,
Sixth Edition</i>. To buy this book, refer to the box to the right.
<ul class="BlueArrows">
<li><a href="getStarted/index.html">Getting Started</a> — An
introduction to Java technology and lessons on installing Java
development software and using it to create a simple program.</li>
<li><a href="java/index.html">Learning the Java Language</a>
— Lessons describing the essential concepts and features of
the Java Programming Language.</li>
<li><a href="essential/index.html">Essential Java Classes</a>
— Lessons on exceptions, basic input/output, concurrency,
regular expressions, and the platform environment.</li>
<li><a href="collections/index.html">Collections</a> —
Lessons on using and extending the Java Collections Framework.</li>
<li><a href="datetime/index.html">Date-Time APIs</a> — How to
use the <tt>java.time</tt> pages to write date and time code.</li>
<li><a href="deployment/index.html">Deployment</a> — How to
package applications and applets using JAR files, and deploy them
using Java Web Start and Java Plug-in.</li>
<li><a href="extra/certification/index.html">Preparation for Java
Programming Language Certification</a> — List of available
training and tutorial resources.</li>
</ul>
<h2>Creating Graphical User Interfaces</h2>
<ul class="BlueArrows">
<li><a href="uiswing/index.html">Creating a GUI with Swing</a>
— A comprehensive introduction to GUI creation on the Java
platform.</li>
<li><a href="https://docs.oracle.com/javafx/index.html">Creating a
JavaFX GUI</a> — A collection of JavaFX tutorials.</li>
</ul>
<h2>Specialized Trails and Lessons</h2>
<p>These trails and lessons are only available as web pages.</p>
<ul class="BlueArrows">
<li><a href="networking/index.html">Custom Networking</a> —
An introduction to the Java platform's powerful networking
features.</li>
<li><a href="ext/index.html">The Extension Mechanism</a> —
How to make custom APIs available to all applications running on
the Java platform.</li>
<li><a href="extra/fullscreen/index.html">Full-Screen Exclusive
Mode API</a> — How to write applications that more fully
utilize the user's graphics hardware.</li>
<li><a href="extra/generics/index.html">Generics</a> — An
enhancement to the type system that supports operations on objects
of various types while providing compile-time type safety. Note
that this lesson is for advanced users. The <a href=
"java/index.html">Java Language</a> trail contains a <a href=
"java/generics/index.html">Generics</a> lesson that is suitable for
beginners.</li>
<li><a href="i18n/index.html">Internationalization</a> — An
introduction to designing software so that it can be easily adapted
(localized) to various languages and regions.</li>
<li><a href="javabeans/index.html">JavaBeans</a> — The Java
platform's component technology.</li>
<li><a href="jdbc/index.html">JDBC Database Access</a> —
Introduces an API for connectivity between the Java applications
and a wide range of databases and data sources.</li>
<li><a href="jmx/index.html">JMX</a>— Java Management
Extensions provides a standard way of managing resources such as
applications, devices, and services.</li>
<li><a href="jndi/index.html">JNDI</a>— Java Naming and
Directory Interface enables accessing the Naming and Directory
Service such as DNS and LDAP.</li>
<li><a href="jaxp/index.html">JAXP</a> — Introduces the Java
API for XML Processing (JAXP) technology.</li>
<li><a href="jaxb/index.html">JAXB</a> — Introduces the Java
architecture for XML Binding (JAXB) technology.</li>
<li><a href="rmi/index.html">RMI</a> — The Remote Method
Invocation API allows an object to invoke methods of an object
running on another Java Virtual Machine.</li>
<li><a href="reflect/index.html">Reflection</a> — An API that
represents ("reflects") the classes, interfaces, and objects in the
current Java Virtual Machine.</li>
<li><a href="security/index.html">Security</a> — Java
platform features that help protect applications from malicious
software.</li>
<li><a href="sound/index.html">Sound</a> — An API for playing
sound data from applications.</li>
<li><a href="2d/index.html">2D Graphics</a> — How to display
and print 2D graphics in applications.</li>
<li><a href="sdp/index.html">Sockets Direct Protocol</a> —
How to enable the Sockets Direct Protocol to take advantage of
InfiniBand.</li>
</ul>
</div><hr />
<div id="Footer"><p class="footertext">
<a href="http://www.oracle.com/corporate/index.html">About Oracle</a> |
<a href="http://www.oracle.com/us/corporate/contact/index.html">Contact Us</a> |
<a href="http://www.oracle.com/us/legal/index.html">Legal Notices</a> |
<a href="http://www.oracle.com/us/legal/terms/index.html">Terms of Use</a> |
<a href="http://www.oracle.com/us/legal/privacy/index.html">Your Privacy Rights</a></p><p class="footertext"><a href="http://www.oracle.com/pls/topic/lookup?ctx=cpyr&id=en-US">
Copyright © 1995, 2019 Oracle and/or its affiliates. All rights reserved.</a></p></div><!-- Start SiteCatalyst code -->
<script type="application/javascript" src="https://www.oracleimg.com/us/assets/metrics/ora_docs.js"></script>
<!-- End SiteCatalyst code -->
</body>
</html>
5.5 操作属性和内容
5.5之前都是读取,那么问题来了-能修改吗?可以啊,不然网页上标签等变颜色,图片咋来的
jsoup 除了可以解析 html/xml 外,还可以进行修改行为。
如图示例,给 P元素 增加class (类属性),并修改内容
public static void main(String[] args) throws Exception {/* File f = new File("c.html"); 直接读取if (!f.exists()) return;*/// 通过类加载器获取资源文件输入流 因为maven项目 放在 resources目录下,用加载器获取InputStream ins = ResourceLoader.class.getClassLoader().getResourceAsStream("d.html");Document doc = Jsoup.parse(ins, "utf-8", "");System.out.println(doc);Element e = doc.select("p").first();e.attr("class", "class1");e.appendText(" Hello JSoup");//原文本追加 文字/文本System.out.println();System.out.println(doc);}