android - 删除 RSS 提要中的 HTML 标签

标签 android html dom tags rss

我有这个 XML 解析器,但它在文本字段中呈现 html 标签,我希望你能用正确的答案帮助编辑我的代码,谢谢。我不知道如何从中删除 HTML 标签。请帮忙,等待接受有效的答案。

      public class XMLParser {

  // constructor
  public XMLParser() {

  }

 /**
 * Getting XML from URL making HTTP request
 * @param url string
 * */
 public String getXmlFromUrl(String url) {
     String xml = null;

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
     }

   /**
    * Getting XML DOM element
   * @param XML string
    * */
  public Document getDomElement(String xml){
    Document doc = null;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is); 



        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }

        return doc;
  }

  /** Getting node value
  * @param elem element
  */
  public final String getElementValue( Node elem ) {
    Node child;
    if( elem != null){
    if (elem.hasChildNodes()){
  for( child = elem.getFirstChild(); child != null; child =  child.getNextSibling() ){
           if( child.getNodeType() == Node.TEXT_NODE  ){
              return child.getNodeValue();
           }
         }
       }
     }
     return "";
   }

  /**
  * Getting node value
  * @param Element node
  * @param key string
  * */
  public String getValue(Element item, String str) {        
        NodeList n = item.getElementsByTagName(str);        
        return this.getElementValue(n.item(0));
   }
      }

最佳答案

如果你想完全删除 html 标签:

方法一)

public String removeHtmlTags(String inStr) {
        int index=0;
        int index2=0;
        while(index!=-1)
        {
            index = inStr.indexOf("<");
            index2 = inStr.indexOf(">", index);
            if(index!=-1 && index2!=-1){
                inStr = inStr.substring(0, index).concat(inStr.substring(index2+1, inStr.length()));
            }
        }
        return inStr;
    }

方法二)

import android.text.Html;

public static String removeHtmlTags(String htmlString){
   //Remove HTML tags
   String noHTMLString = Html.fromHtml(htmlString).toString();        
   return noHTMLString;
}

您需要在此方法中调用 removeHtmlTags():

public final String getElementValue( Node elem ) {
    Node child;
    if( elem != null){
        if (elem.hasChildNodes()){
            for( child = elem.getFirstChild(); child != null; child =  child.getNextSibling() )                          {
                if( child.getNodeType() == Node.TEXT_NODE  ){
                                            //removeHtmlTags()
                    return removeHtmlTags(child.getNodeValue());
                }
            }
        }
    }
    return "";
}

public String getValue(Element item, String str) {        
    NodeList n = item.getElementsByTagName(str);      
            //removeHtmlTags()  
    return removeHtmlTags(this.getElementValue(n.item(0)));
}

关于android - 删除 RSS 提要中的 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21940554/

相关文章:

android - 如何为 Android 应用程序查找城市的 GeoPoint 坐标

jquery - 如何显示和隐藏一个div

php - 使用 PHP 读取 HTML5 输入(类型颜色)值

javascript - 未捕获的类型错误 : Cannot set property 'className' of null Error

javascript - 通过单击 HTML 中的链接覆盖 JS 变量

android - 当路径上的第一项为空时,与路径的绑定(bind)不起作用

java.lang.IndexOutOfBoundsException : Invalid index 2, Arraylist 中的大小为 2?

javascript - HTML 表中删除的行保留在 ReactDOM 中

android - 为 crouton 库使用自定义布局

html - 尽管有多个网格元素,为什么我的网格容器没有展开?