【问题标题】:remove html tag in android删除android中的html标签
【发布时间】:2013-02-18 05:41:59
【问题描述】:

我有以下 XML 提要:

<Description>
  <p>Touch, tap, flip, slide! You don&#39;t just read Books, you experience it.</p>
</Description>

这里我必须显示类似

的描述

触摸、点击、翻转、滑动!你不 39.just read the Books, you experience it.

在这里我处理了解析器:

   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;

    }

在 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);}

现在我必须运行该应用程序,这意味着 html 标记与我的描述一起显示...在这里如何删除 HTML 标记?请为我提供解决方案???

我希望显示没有 Html 标记的描述...请为这些提供解决方案。

编辑:

    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);}

编辑:

  public static String html2text(String html) {
  return Jsoup.parse(html).text();
    }

在 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);}

但我没有得到 o/p..请为我提供解决方案???如何删除这些描述中的 html 标签...

【问题讨论】:

    标签: java android html xml-parsing stringbuffer


    【解决方案1】:

    一种选择是添加JSoup 库,将其导入并按如下方式使用:

    public static String html2text(String html) {
    return Jsoup.parse(html).text();
    }
    

    【讨论】:

      【解决方案2】:

      据我所知,您可以通过可跨接口获取数据。

      试试这个:

      跨度可跨度 = Html.fromHtml(arrayList.get(position).getBusinessDescription());
      System.out.println("说明 value----->"+n+att_ID+"------>>"+spannable);

      查看以下链接了解更多详情:

      http://developer.android.com/reference/android/text/Spanned.html http://developer.android.com/reference/android/text/Spannable.html

      【讨论】:

        【解决方案3】:

        您可以使用 Android 中的内置 HTML 类轻松删除 Android 中的任何 HTML 标记。导入android.text.Html;。现在,考虑到“数据”是您的具有 HTML 标记的字符串变量,您可以使用 Html.fromHtml(data).toString() 来取回没有任何 HTML 标记的字符串。

        【讨论】:

          【解决方案4】:
          String plain = Html.fromHtml("your_html_string").toString();
          

          【讨论】:

            【解决方案5】:

            删除 html 的简单方法。这将返回非 html 格式的文本

             private String removeHtml(String html) {
                html = html.replaceAll("<(.*?)\\>"," ");
                html = html.replaceAll("<(.*?)\\\n"," ");
                html = html.replaceFirst("(.*?)\\>", " ");
                html = html.replaceAll("&nbsp;"," ");
                html = html.replaceAll("&amp;"," ");
                return html;
            }
            

            根据html标签格式化并移除标签。

            Html.fromHtml(data).toString();
            

            【讨论】:

              【解决方案6】:

              只需添加这几行代码即可完成。

              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);
              

              【讨论】:

                【解决方案7】:
                    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.
                
                                            }
                

                希望这会有所帮助:)

                【讨论】:

                  【解决方案8】:
                  //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")
                   )
                  );
                  

                  【讨论】:

                  • 嗨何塞!这可能是一个很好的答案,您能否详细说明并向提问者解释一些步骤?这将大有帮助。
                  猜你喜欢
                  • 1970-01-01
                  • 2018-02-10
                  • 2017-07-08
                  • 1970-01-01
                  • 1970-01-01
                  • 2023-03-15
                  • 2013-08-19
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多