【问题标题】:Removing HTML Tags in RSS feed删除 RSS 提要中的 HTML 标签
【发布时间】:2014-02-21 17:01:20
【问题描述】:

我有这个 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));
   }
      }

【问题讨论】:

    标签: android html dom tags rss


    【解决方案1】:

    如果你想完全删除 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)));
    }
    

    【讨论】:

    • 感谢您的回复,我会尝试并立即回复您
    • 我将它添加到我的 XML Parser 类中,但它不起作用
    • 我使用这种方法,例如我有“aad Elenasys is one jump in tree lorem ipsum”;然后我只得到“aad Elenasys 是 tree lorem ipsum 的一次跳跃”。你有: return removeHtmlTags(this.getElementValue(n.item(0))); ???我使用 REGEX 添加了其他方法。
    • 谢谢,这个方法是我添加到XML Parser类中
    • 不,我没有:返回 removeHtmlTags(this.getElementValue(n.item(0)));很抱歉,但我是个菜鸟,你能告诉我如何将它添加到上面的 XML 解析器中,还是我不打算在那里添加它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    相关资源
    最近更新 更多