remove html tag in android

2019-02-19 04:21发布

I have follows below XML feed:

<Description>
  <p>Touch, tap, flip, slide! You don&#39;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.

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])>.*?</\1>", "");
// Remove New line from java string and replace html break
noHTMLString = noHTMLString.replaceAll("\n", " ");
noHTMLString = noHTMLString.replaceAll("\"", "&quot;");
noHTMLString = noHTMLString.replaceAll("<(.*?)\\>"," ");//Removes all items in brackets
noHTMLString = noHTMLString.replaceAll("<(.*?)\\\n"," ");//Must be undeneath
noHTMLString = noHTMLString.replaceFirst("(.*?)\\>", " ");
noHTMLString = noHTMLString.replaceAll("&nbsp;"," ");
noHTMLString = noHTMLString.replaceAll("&amp;"," ");
return noHTMLString;

    }

In 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 ???

i wish to display the description without Html tags...please provide e solution for these.

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:

    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...

7条回答
家丑人穷心不美
2楼-- · 2019-02-19 04:53

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("&nbsp;"," ");
            html = html.replaceAll("&amp;"," ");
            newsModel.setNews(html);
            Log.d("originaltext: ",html);
查看更多
登录 后发表回答