将html转成xhtml
HTML指的是超文本标记语言 (Hyper Text Markup Language),html不强制要求每个标记都是关闭的,比如img,你可以写成[外链图片转存失败(img-oQ5OgJzg-1562220416236)(https://mp.csdn.net/xxx/yyy.jpg)]。换行符br,可以写成
。
但有时,html需要转成其他文档(如doc、pdf)时,需要html是标准的、闭合的html,也就是可扩展的超文本标记语言(XHTML)。这时,需要将html转换为XHTML。
这里介绍两种html转换xhtml的方法。
一、使用JTidy
JTidy是一个html检查/格式化输出/Dom解析工具。
官网:http://jtidy.sourceforge.net/index.html
1、首先你需要下载JTidy,
下载地址:http://mvnrepository.com/artifact/net.sf.jtidy/jtidy/r938
也可以使用基于maven开发,maven依赖:
1
2
3
4
5
6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class HtmlToXHtmlJtidy {
public static String html2xhtml(String html) {
ByteArrayInputStream stream = new ByteArrayInputStream(html.getBytes());
ByteArrayOutputStream tidyOutStream = new ByteArrayOutputStream();
// 实例化Tidy对象
Tidy tidy = new Tidy();
// 设置输入
tidy.setInputEncoding("utf-8");
// 如果是true 不输出注释,警告和错误信息
tidy.setQuiet(true);
// 设置输出
tidy.setOutputEncoding("utf-8");
// 不显示警告信息
tidy.setShowWarnings(false);
// 缩进适当的标签内容。
tidy.setIndentContent(true);
// 内容缩进
tidy.setSmartIndent(true);
tidy.setIndentAttributes(false);
// // 只输出body内部的内容
// tidy.setPrintBodyOnly(true);
// 多长换行
tidy.setWraplen(1024);
// 输出为xhtml
tidy.setXHTML(true);
// 去掉没用的标签
tidy.setMakeClean(true);
// 清洗word2000的内容
tidy.setWord2000(true);
// 设置错误输出信息
tidy.setErrout(new PrintWriter(System.out));
tidy.parse(stream, tidyOutStream);
return tidyOutStream.toString();
}
public static void main(String[] args) throws Exception {
File file = new File("E:\\mywork\\sample\\html2xhtml\\html2xhtml.html");
FileInputStream input = new FileInputStream(file);
int size = input.available();
byte[] buff = new byte[size];
input.read(buff);
input.close();
String html = new String(buff, "utf-8");
System.out.println("============html===================");
System.out.println(html);
String xhtml = HtmlToXHtmlJtidy.html2xhtml(html);
System.out.println("============xhtml===================");
System.out.println(xhtml);
}
}
3、测试
测试一下效果。
转换前html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
html转xhtml测试
[外链图片转存失败(img-SoCtjc0d-1562220416237)(https://mp.csdn.net/img/sg2.jpg)]
测试如何将html转换成xhtml
其中,黄色背景的都是不标准的标记。执行后结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<title>html转xhtml测试</title>
html转xhtml测试
[外链图片转存失败(img-bq4Z5Uyg-1562220416237)(https://mp.csdn.net/img/sg2.jpg)]
<p>测试如何将html转换成xhtml</p>
其中,三处出错的地方都纠正过来。
二、使用Jsoup
下面再让我们看下一种对应方法。就是使用Jsoup。
jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据。官网:https://jsoup.org/
1、下载Jsoup。
下载地址
http://mvnrepository.com/artifact/org.jsoup/jsoup/1.10.2
也可以使用基于maven开发,maven依赖:
1
2
3
4
5
6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class HtmlToXHtmlJsoup {
public static String html2xhtml(String html) {
Document doc = Jsoup.parse(html);
doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);
return doc.html();
}
public static void main(String[] args) throws Exception {
File file = new File("E:\\mywork\\sample\\html2xhtml\\html2xhtml.html");
FileInputStream input = new FileInputStream(file);
int size = input.available();
byte[] buff = new byte[size];
input.read(buff);
input.close();
String html = new String(buff, "utf-8");
System.out.println("============html===================");
System.out.println(html);
String xhtml = HtmlToXHtmlJsoup.html2xhtml(html);
System.out.println("============xhtml===================");
System.out.println(xhtml);
}
}
3、测试
转换前同上。
转换后:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
html转xhtml测试
[外链图片转存失败(img-FhffTpNV-1562220416237)(https://mp.csdn.net/img/sg2.jpg)]
测试如何将html转换成xhtml