java 在android中删除html标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15196414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove html tag in android
提问by user2098063
I have follows below XML feed:
我遵循以下 XML 提要:
<Description>
<p>Touch, tap, flip, slide! You don't just read Books, you experience it.</p>
</Description>
Here I have to display the description like
在这里我必须显示这样的描述
Touch,tap,flip,slide! You don 39.just read the Books, you experience it.
触摸,点击,翻转,滑动!你不 39. 只看书,你就体验它。
Here I have handled the parser like:
在这里,我处理了解析器,如:
public static String removeHTML(String htmlString)
{
// Remove HTML tag from java String
String noHTMLString = htmlString.replaceAll("\<.*?\>", "");
// Remove Carriage return from java String
noHTMLString = noHTMLString.replaceAll("\r", "<br/>");
noHTMLString = noHTMLString.replaceAll("<([bip])>.*?</>", "");
// Remove New line from java string and replace html break
noHTMLString = noHTMLString.replaceAll("\n", " ");
noHTMLString = noHTMLString.replaceAll("\"", """);
noHTMLString = noHTMLString.replaceAll("<(.*?)\>"," ");//Removes all items in brackets
noHTMLString = noHTMLString.replaceAll("<(.*?)\\n"," ");//Must be undeneath
noHTMLString = noHTMLString.replaceFirst("(.*?)\>", " ");
noHTMLString = noHTMLString.replaceAll(" "," ");
noHTMLString = noHTMLString.replaceAll("&"," ");
return noHTMLString;
}
In endElement :
在 endElement 中:
public void endElement(String uri, String localName, String qName)throws SAXException {
currentElement = false;
if (localName.equalsIgnoreCase("Description")){
sitesList.setDescription(currentValue);
String Sub_arry=n+currentValue;
Appscontent.Sub_arraylistdes.add(Sub_arry);
String stringWithoutHTML=removeHTML(currentValue);
System.out.println("description value----->"+n+att_ID+"------>>"+stringWithoutHTML);}
Now i have to run the app means the html tag is displayed with my description...Here how can I remove the HTML tag? please provide me solution for these ???
现在我必须运行该应用程序意味着 html 标签与我的描述一起显示...这里我如何删除 HTML 标签?请为我提供解决方案???
i wish to display the description without Html tags...please provide e solution for these.
我希望显示没有 Html 标签的描述...请为这些提供电子解决方案。
EDIT:
编辑:
if (localName.equalsIgnoreCase("Description")){
sitesList.setDescription(currentValue);
String Sub_arry=n+currentValue;
StringBuffer sb = new StringBuffer();
sb.append(Sub_arry);
String newString = sb.toString();
Appscontent.Sub_arraylistdes.add(newString);
System.out.println("description value----->"+n+att_ID+"------>>"+newString);}
EDIT:
编辑:
public static String html2text(String html) {
return Jsoup.parse(html).text();
}
In endElement:
在 endElement 中:
if (localName.equalsIgnoreCase("Description")){
sitesList.setDescription(currentValue);
String Sub_arry=n+currentValue;
Appscontent.Sub_arraylistdes.add(Sub_arry);
String stringWithoutHTML=html2text(currentValue);
System.out.println("description value----->"+n+att_ID+"------>>"+stringWithoutHTML);}
But i didn't get the o/p..pls provide me solution for these ??? how can i remove the html tags in these description...
但我没有得到 o/p..pls 为我提供这些解决方案???我如何删除这些描述中的 html 标签...
回答by contactsunny
You can easily remove any HTML tag in Android using the built-in HTML class in Android. Import android.text.Html;
. Now, considering "data" is your String variable which has HTML tags, you use Html.fromHtml(data).toString()
to get back the string without any HTML tags.
您可以使用 Android 中的内置 HTML 类轻松删除 Android 中的任何 HTML 标记。进口android.text.Html;
。现在,考虑到“数据”是具有 HTML 标签的 String 变量,您可以Html.fromHtml(data).toString()
用来获取没有任何 HTML 标签的字符串。
回答by Akanksha Rathore
Simple method to remove html. This will return non-html formatted text
删除html的简单方法。这将返回非 html 格式的文本
private String removeHtml(String html) {
html = html.replaceAll("<(.*?)\>"," ");
html = html.replaceAll("<(.*?)\\n"," ");
html = html.replaceFirst("(.*?)\>", " ");
html = html.replaceAll(" "," ");
html = html.replaceAll("&"," ");
return html;
}
To formatted according html tag and remove tag.
根据 html 标签格式化并删除标签。
Html.fromHtml(data).toString();
回答by drew moore
回答by Rohit Mandiwal
String plain = Html.fromHtml("your_html_string").toString();
回答by Tarit Ray
private int iMobileVersion = Build.VERSION.SDK_INT;
String strResonseTemplate = data.getStringExtra("template"); //getting HTML data in string
if (iMobileVersion >= 24) {
mEtReply.setText(Html.fromHtml(strResonseTemplate, Html.FROM_HTML_MODE_COMPACT));// this code only works on and above API 24, and removes all HTML tag, but gives same view as HTML in Edittext.
} else {
mEtReply.setText(Html.fromHtml(strResonseTemplate).toString()); // bellow API level 24 we are removing only HTML tags, it will show as normal text.
}
Hope this will be helpfull :)
希望这会有所帮助:)
回答by Jose Antonio Dominguez Garcia
//Patter to detect elements contained into "<>"
private static final Pattern REMOVE_TAGS = Pattern.compile("<.+?>");
//Method to remove the html tags contained in a String variable
public static String removeTags(String string)
{
//validate that at least one value contains the string
if (string == null || string.length() == 0)
{
return string;
}
//Function to find the matches within the chain and the pattern
Matcher m = REMOVE_TAGS.matcher(string);
//replace <> element with ""
return m.replaceAll("");
}
//Implementation of the method to eliminate html tags and place inside a Text control
this.itemView.setText(
Html.fromHtml(
new String(removeTags("<h1>My Title here</h1>").getBytes("ISO-8859-1"),"UTF-8")
)
);
回答by Hanisha
Just add these few lines of code and you are done .
只需添加这几行代码即可完成。
String html=(jsonObject1.getString("originaltext"));
html = html.replaceAll("<(.*?)\>"," ");
html = html.replaceAll("<(.*?)\\n"," ");
html = html.replaceFirst("(.*?)\>", " ");
html = html.replaceAll(" "," ");
html = html.replaceAll("&"," ");
newsModel.setNews(html);
Log.d("originaltext: ",html);
回答by Maulik
According to my knowledge you can get data by spannable interface.
据我所知,您可以通过可跨接口获取数据。
Try using this:
尝试使用这个:
Spanned spannable = Html.fromHtml(arrayList.get(position).getBusinessDescription());
System.out.println("description value----->"+n+att_ID+"------>>"+spannable);
Spanned spannable = Html.fromHtml(arrayList.get(position).getBusinessDescription());
System.out.println("描述值----->"+n+att_ID+"------>>"+spannable);
check the below link for more details:
查看以下链接了解更多详情:
http://developer.android.com/reference/android/text/Spanned.htmlhttp://developer.android.com/reference/android/text/Spannable.html
http://developer.android.com/reference/android/text/Spanned.html http://developer.android.com/reference/android/text/Spannable.html