简体   繁体   中英

parse the html and show it in webview android

I have a html text in

 <html>
 <head></head>
 <body>
  &lt;div id="carousel-generic" class="banner-erbj carousel slide" data-ride="carousel"&gt; &lt;ul class="carousel-indicators"&gt; &lt;li class="active" data-target="#carousel-generic" data-slide-to="0"&gt;0&lt;/li&gt; &lt;li data-target="#carousel-generic" data-slide-to="1"&gt;0&lt;/li&gt; &lt;/ul&gt; &lt;div class="carousel-inner"&gt; &lt;div class="item active"&gt;&lt;img src="imagesrcpath" alt="" /&gt;&lt;/div&gt; &lt;div class="item"&gt;&lt;img src="imagesrcpath" alt="" /&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; 
</html>
 </body>

I want to take out the link in it and display in a webview. I have tried the jsoup method and some solutions provided in the questions on stackoverflow also but not able to find anysolution .. please help

After lot more of digging i found the solution to it :

 Spanned parsed = Html.fromHtml(text);
 String finalstr = ("<html><body>").concat(parsed.toString()).concat("</body></html>"); 

mWebView.setInitialScale(30);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setLoadWithOverviewMode(true);
    /*mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);*/
    mWebVie.getSettings().setUseWideViewPort(true);
    mWebView.loadData(finalstr, "text/html", "UTF-8");

This whole set of instructions helped me do the same.

Use regex.

<img.+?/>

will simply find image tags.

Learn XPATH

http://www.w3schools.com/xml/xpath_intro.asp

and use your xpath knowledge in

http://htmlcleaner.sourceforge.net/ , to get whatever you want from html

You can take advantage of the power of REGEX here.

Pattern pattern = Pattern.compile("/src=\"(.*)\"/");
Matcher matcher = pattern.matcher(YOURHTML);
matcher.group(1); // this way you can read all of the srcs

Disclaimer: the regex above is not tested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM