문제

I am using JSOUP library to parse the html.But I was ending up having extra closing tags with encoded <>(&lt and &gt) added up in my DOM.Hence I used the String utils library to get rid of that encoded stuff.Hence I still have duplicate closing tags but they are not encoded. So my initial html is

<!DOCTYPE html>
<html xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" class="SAF" id="global-header-light">
<head>

</head>
<body>


<div style="background-image: url(http://aka-cdn-ns.adtech.de/rm/ads/23274/HPWomenLOFT_1381687318.jpg);background-repeat: no-repeat;-webkit-background-size: 1001px 2059px; height: 2059px; width: 1001px; text-align: center; margin: 0 auto;">                      

<div style="height:2058px; padding-left:0px; padding-top:36px;">


<iframe style="height:90px; width:728px;" />



</div>
</div>

</body>
</html>

And after getiing it through this code

String url = request.getParameter("htmluri").trim();
        System.out.println("Fetching %s..."+url);

        Document doc = Jsoup.connect(url).get();
        //System.out.println(doc.html());
        Document.OutputSettings settings = doc.outputSettings();
        settings.prettyPrint(false);
        //settings.escapeMode(Entities.EscapeMode.base);
        settings.charset("ASCII");
        String html = doc.html();
        html = StringEscapeUtils.unescapeHtml(html);
        System.out.println(html);

       // String html = doc.html();

        System.out.println(html);

I get this html

<!DOCTYPE html><html xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" class="SAF" id="global-header-light"><head>

</head>
<body>


<div style="background-image: url(aol.jpeg);background-repeat: no-repeat;-webkit-background-size:100720; height:720; width:100; text-align: center; margin: 0 auto;">                      

<div style="height:100; padding-left:0px; padding-top:36px;">


<iframe style="height:90px; width:728px;"></iframe>



</div>
</div>

</body>
</html></div></div></body></html>

So there are additional duplicate closing div body and html tags.Though they dont harm the rendering of page I guess.Is there a way to get rid of it.

Thanks Swaraj

도움이 되었습니까?

해결책

And I'm back :P . You just parse the html that you have again so jsoup will remove any extra closing tags

String url = request.getParameter("htmluri").trim();
System.out.println("Fetching %s..."+url);

Document doc = Jsoup.connect(url).get();
Document.OutputSettings settings = doc.outputSettings();
settings.prettyPrint(false);
settings.charset("ASCII");
String html = doc.html();
html = StringEscapeUtils.unescapeHtml(html);
html = Jsoup.parse(html).html();   //This will take care of any extra closing tags 
System.out.println(html);

Output

Fetching %s...http://iqtestsites.adtech.de/pictelatest/custombkgd/StylelistDevil.html
<!DOCTYPE html>
<html xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" class="SAF" id="global-header-light">
 <head> 
  <style>

</style> 
 </head> 
 <body> 
  <div style="background-image: url(http://iqtestsites.adtech.de/pictelatest/custombkgd/StylelistDevil.jpg); background-repeat: no-repeat;-webkit-background-size: 1001px 1903px;height: 1903px; width: 1001px; text-align: center; margin: 0 auto;"> 
   <div style="height:1050px; width:300px; padding-left:681px; padding-top:200px;"> 
    <iframe style="height:1050px; width:300px;"></iframe> 
   </div> 
  </div>  
 </body>
</html>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top