HTML指的是超文本标记语言 (Hyper Text Markup Language),html不强制要求每个标记都是关闭的,比如img,你可以写成<img src="xxx/yyy.jpg" >。换行符br,可以写成<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依赖:
<!-- https://mvnrepository.com/artifact/net.sf.jtidy/jtidy -->
<dependency>
<groupId>net.sf.jtidy</groupId>
<artifactId>jtidy</artifactId>
<version>r938</version>
</dependency>
2、代码
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>html转xhtml测试 </title>
<script type="text/javascript" src="jquery-3.2.1.js"></script>
</head>
<body>
<h1>html转xhtml测试</h1>
<br>
<img src="img/sg2.jpg" style="max-width:200px;">
<p>测试如何将html转换成xhtml
</body>
</html>
其中,黄色背景的都是不标准的标记。
执行后结果:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy for Java (vers. 2009-12-01), see jtidy.sourceforge.net" />
<meta charset="utf-8" />
<title>html转xhtml测试</title>
<script type="text/javascript" src="jquery-3.2.1.js">
//<![CDATA[
//]]>
</script>
</head>
<body>
<h1>html转xhtml测试</h1>
<br />
<img src="img/sg2.jpg" style="max-width:200px;" />
<p>测试如何将html转换成xhtml</p>
</body>
</html>
其中,三处出错的地方都纠正过来。
二、使用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依赖:
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
2、代码
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、测试
转换前同上。
转换后:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>html转xhtml测试 </title>
<script type="text/javascript" src="jquery-3.2.1.js"></script>
</head>
<body>
<h1>html转xhtml测试</h1>
<br />
<img src="img/sg2.jpg" style="max-width:200px;" />
<p>测试如何将html转换成xhtml </p>
</body>
</html>