Android - 使用标记提取html并对结果

时间:2018-05-21 09:37:55

标签: android jsoup html-parsing

我有一个JSON对象,它返回一个带有html内容的长字符串, 无论如何我可以从这个html字符串中获取某些文本并将其分配到textview中吗?我想得到的可能是<h1>&amp; <p>并抛弃所有其他人。

&#13;
&#13;
\r\n<div class=\"page-title-wrap sx-hide\">\r\n    
<div class=\"page-title clearfix\">\r\n        
<div class=\"col-lg-12\">\r\n  <h1>Latest Deals</h1>\r\n </div>\r\n  </div>\r\n
</div>\r\n\r\n<div class=\"breadcrumb-wrapper\">\r\n    
<ul class=\"breadcrumb\">\r\n        
<li><a href=\"/Home\">Home</a></li>\r\n        
<li><a href=\"/Deals\">Deals</a></li>\r\n        
<li class=\"active\">Great promotion! Is now RM 95 only!
</li>\r\n    
</ul>\r\n
</div>\r\n\r\n
<div class=\"article outer clearfix\">\r\n    
<div class=\"col-sm-12\">\r\n        
<img alt=\"" title=\"Great promotion! Is now RM 95 only!\" src=\"">\r\n        
<h1>Great promotion! Is now RM 95 only!</h1>\r\n        
<p class=\"date\">March 28th, 2017</p>\r\n        
<p><strong class=\"text-red\"></strong></p>\r\n        
<p>This is the paragraht that shows the description of the promotion deals. You can write anything here.\r\n </p>\r\n        
<p>The buses offered by Alisan Golden Coach are in single deck or double deck. All of the buses are equip with air-conditioning and comfortable seats to ensure passengers are comfortable while travelling on the long journeys.</p>\r\n\r\n        
<p>Book your bus ticket before too late and enjoy the great saving.</p>\r\n\r\n\r\n\r\n\r\n\r\n\r\n        
<div class=\"m-top30 m-bottom20\">\r\n    
<a href=\"/home\" class=\"btn btn-lg btn-orange\">Home</a>\r\n\r\n    \r\n\r\n\r\n</div>\r\n\r\n\r\n
<div id=\"fb-root\"></div>\r\n<script>\r\n    (function(d, s, id) {\r\n      var js, fjs = d.getElementsByTagName(s)[0];\r\n      if (d.getElementById(id)) return;\r\n      js = d.createElement(s); js.id = id;\r\n      js.async = true;\r\n      js.src = '';\r\n      fjs.parentNode.insertBefore(js, fjs);\r\n    }(document, 'script', 'facebook-jssdk'));</script>\r\n\r\n
<div class=\"fb-share-button\" data-href=\"http://google.com/\" data-layout=\"button_count\" data-size=\"large\" data-mobile-iframe=\"true\">\r\n    
<a target=\"_blank\" href=\"" class=\"fb-xfbml-parse-ignore\">Share</a>\r\n</div>\r\n    </div>\r\n</div>
&#13;
&#13;
&#13;

基本上,<h1>内容将分配给&#34;标题&#34; textview和我想从第一个<p>和最后</p>(div类之前的最后一个/ p)中获取内容,将其放入webview中,因为我希望有段落排列。

====================编辑====================
我已经使用Jsoup成功获取内容,意识到使用.text()将获取带有标记的所有元素并放入一行,但是,我希望结果在加载时保持<p>格式HTML视图。我有什么想法吗?

Document doc = Jsoup.parse(content); Elements eTitle = doc.getElementsByTag("h1"); Elements eBody = doc.getElementsByTag("p"); binding.fragmentWebview.loadData("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" + eBody.text(), "text/html; charset=utf-8","UTF-8");

0 个答案:

没有答案