我通过执行以下操作将一些Html文本从网页转换为字符串
mydescription =Html.fromHtml(data.getBody()).toString();
这是data.getBody()
返回的内容: -
<div><p>It's great to have great dynamic companies to work with, and NXP is no exception.</p><p><img alt="This is an image of NXP Logo" src="https://anprodstorage.blob.core.windows.net/b75ef288-0381-45c4-a4cd-809097370bec/untitled.png" style="margin:5px;" /><br></p><div><iframe width="560" height="315" src="https://www.youtube.com/embed/I6191gXXGog" frameborder="0"></iframe> </div><p><br></p></div>
但在该html文本中也有一个图像源。当我这样做时,我得到一个方形图像,其中写有obj而不是图像。
这是myDescription

我只想获得文字,而不是图片。
我如何获取文本而不是图像
答案 0 :(得分:0)
我建议在使用HTML时使用库jsoup(使用汤,只能通过调用Jsoup.parse(html).text()
来获取文本)
Haven自己尝试过
private static final Pattern REMOVE_TAGS = Pattern.compile("<img>(\\S+)</img>");
public static String removeTags(String string) {
if (string == null || string.length() == 0) {
return string;
}
Matcher m = REMOVE_TAGS.matcher(string);
return m.replaceAll("");
}
如果要删除所有HTML代码,则可以使用:
replaceAll("\\<[^>]*>","")
关于你的第二个问题(来自来源2):
// the pattern we want to search for
Pattern p = Pattern.compile("<p>(\\S+)</p>");
Matcher m = p.matcher(string);
// if we find a match, get the group
if (m.find())
{
// get the matching group
String codeGroup = m.group(1);
// print the group
System.out.format("'%s'\n", codeGroup);
}
答案 1 :(得分:0)
使用此代码:
String clippedBody = htmlString.replaceAll("<img[^>]*?>.*?/[^>]*?>", "");
答案 2 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
String htmlString = "<div><p>It's great to have great dynamic companies to work with, and NXP is no exception.</p><p><img alt=\"This is an image of NXP Logo\" src=\"https://anprodstorage.blob.core.windows.net/b75ef288-0381-45c4-a4cd-809097370bec/untitled.png\" style=\"margin:5px;\" /><br></p><div><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/I6191gXXGog\" frameborder=\"0\"></iframe> </div><p><br></p></div>";
String first = htmlString.substring(0,htmlString.indexOf("<img"));
String second = htmlString.substring(htmlString.indexOf("/>",htmlString.indexOf("<img"))+2,htmlString.length());
textview.setText(Html.fromHtml(first+second));