1. 程式人生 > >將html轉成xhtml

將html轉成xhtml

工具 兩種 odi 縮進 generator println encoding 操作方法 方法

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>

將html轉成xhtml