如何避免在Jsoup解析中的HTML头标签 [英] How to avoid surrounding html head tags in Jsoup parse

查看:218
本文介绍了如何避免在Jsoup解析中的HTML头标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Jsoup,我尝试解析给定的html内容。在Jsoup.parse()之后,html输出将html,head和body标签附加到输入。



样本输入:
$ b

 < p>< b>这< i>是< / i>< / b> < i>我的句子< / i>的文字。< / p> 

Java代码:

  import java.io.File; 
import java.io.IOException;

导入org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HTMLParse {

public static void main(String args [])throws IOException {
try {
File input = new File( /ab.html);
String html = FileUtils.readFileToString(input,null);

Document doc = Jsoup.parseBodyFragment(html);
doc.outputSettings()。prettyPrint(false);
System.out.println(doc.html());
}
catch(Exception e){
e.printStackTrace();



code

$ b实际输出:

 < html>< head>< / head>< body>< p><< ; b>此< i>是< / i>< / b> < i>我的句子< / i>的文字。< / p> 
< / body>< / html>

预期成果:

 < p>< b>这个< i>是< / i>< / b> < i>我的句子< / i>的文字。< / p> 

请协助。

解决方案原因:

parseBodyFragment()以及其他所有 parse() - 方法通过 default 使用 HTML解析器 。那些添加总是 HTML-Shell(< html> ...< / html> < head> ...< / head> 等)。

解决方案:

请不要使用HTML解析器,而应使用 XML解析器 ; - )

  Document doc = Jsoup.parse(html,,Parser.xmlParser()); 

替换单行并解决问题。

示例:

  final String html =< p>< b>这个< i>是< / i>< / b>< i>我的文章< / i>文字。< / p>; 

Document docHtml = Jsoup.parse(html);
Document docXml = Jsoup.parse(html,,Parser.xmlParser());

System.out.println(******* HTML ******* \\\
+ docHtml);
System.out.println();
System.out.println(******* XML ******* \\\
+ docXml);

输出:

  ******* HTML ******* 
< html>
< head>< / head>
< body>
< p>< b>这个< i>是< / i>< / b> < i>我的句子< / i>的文字。< / p>
< / body>
< / html>

******* XML *******
< p>< b>此< i>是< / i>< / b> < i>我的句子< / i>的文字。< / p>


Using Jsoup i try to parse the given html content. After Jsoup.parse() the html output append html, head and body tag to the input. I just want to ignore these.

Sample Input:

<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>

Java code:

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HTMLParse {

    public static void main(String args[]) throws IOException {
        try{
            File input = new File("/ab.html");
            String html = FileUtils.readFileToString(input, null);

            Document doc = Jsoup.parseBodyFragment(html);
            doc.outputSettings().prettyPrint(false);
            System.out.println(doc.html());
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}

Actual output:

<html><head></head><body><p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>
    </body></html>

Expected Output:

<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>

Please help.

解决方案

The cause:

parseBodyFragment() as well as all other parse()-methods use a HTML parser by default. And those add always the HTML-Shell (<html>…</html>, <head>…</head> etc.).

The Solution:

Just don't use a HTML-parser, use a XML-parser instead ;-)

Document doc = Jsoup.parse(html, "", Parser.xmlParser());

Replace that single line and your problem is solved.

Example:

final String html = "<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>";

Document docHtml = Jsoup.parse(html);
Document docXml = Jsoup.parse(html, "", Parser.xmlParser());

System.out.println("******* HTML *******\n" + docHtml);
System.out.println();
System.out.println("*******  XML *******\n" + docXml);

Output:

******* HTML *******
<html>
 <head></head>
 <body>
  <p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>
 </body>
</html>

*******  XML *******
<p><b>This <i>is</i></b> <i>my sentence</i> of text.</p>

这篇关于如何避免在Jsoup解析中的HTML头标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆