user1662302
user1662302

Reputation:

Removing HTML Tags in RSS feed

I have this XML Parser but it rendering html tags in the textfield, i would like you to help edit my code with the correct answer, thank you. i don't know how to remove HTML Tags from it. Please help, waiting to accept the answer that works.

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

Upvotes: 1

Views: 1947

Answers (1)

Jorgesys
Jorgesys

Reputation: 126465

if you want completely remove the html tags:

Method 1)

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

Method 2)

import android.text.Html;

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

You need to call removeHtmlTags() inside this methods:

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

Upvotes: 1

Related Questions