2012-03-12 12 views
1

私のプロジェクトでは、XMLを解析する必要があります。 XMLの一部の項目にはHTMLタグがあります。私はそれらのタグを削除しようとしたが、私は成功しませんでした。活動のコードは次のとおりです。Android、XMLを解析する、HTMLタグを無視する方法

private NewsFeedItemList parseNewsContent() { 
     NewsParserHandler newsParserHandler = null; 

     Log.i("NewsList", "Starting to parse XML..."); 

     try { 
      SAXParserFactory factory = SAXParserFactory.newInstance(); 
      SAXParser parser = factory.newSAXParser(); 
      XMLReader xr = parser.getXMLReader(); 
      newsParserHandler = new NewsParserHandler(); 
      xr.setContentHandler(newsParserHandler); 

      ByteArrayInputStream is = new ByteArrayInputStream(strServerResponseMsg.getBytes()); 
      xr.parse(new InputSource(is)); 

     } catch (ParserConfigurationException e) { 
      e.printStackTrace(); 
     } catch (SAXException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     NewsFeedItemList itemList = newsParserHandler.getNewsList(); 
//  checkLog(itemList); 

     Log.i("NewsList", "Parsing XML finished. Sending result back to caller..."); 
     return itemList; 
    } 

「strServerResponseMsgは」完全に、私はすべての項目を解析するカムが、htmlタグを持っている人は解析できないだろうXML情報(http://www.mania.com.my/rss/ManiaTopStoriesFeedFull.aspx?catid=146

の含まれています。

これは私のパーサハンドラです:

public class NewsParserHandler extends DefaultHandler { 

    private NewsFeedItemList newsFeedItemList; 
    private boolean current = false; 
    private String currentValue = null; 

    /* Because the feed has another "Title", "link" and "pubdate" name in root, 
    * we need to don't let to be stored in arrays. Therefore, we ignore all of 
    * them by incrementing count.*/ 
    private int count = 0; 


    @Override 
    public void characters(char[] ch, int start, int length) throws SAXException { 
     super.characters(ch, start, length); 

     if(current) { 
      currentValue = new String(ch, start, length); 

      if(currentValue==null || currentValue=="" || currentValue==" ") 
       currentValue = "-"; 

      current = false; 
     } 
    } 

    @Override 
    public void startDocument() throws SAXException { 
     super.startDocument(); 

     newsFeedItemList = new NewsFeedItemList(); 
    } 

    @Override 
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 
     super.startElement(uri, localName, qName, attributes); 

     current = true; 
    } 

    @Override 
    public void endElement(String uri, String localName, String qName) throws SAXException { 
     super.endElement(uri, localName, qName); 

     current = false; 

     if(localName.equals("title")) { 
      if(count >= 1) 
       newsFeedItemList.setTitle(currentValue); 
     } 
     if(localName.equals("description")) { 
      newsFeedItemList.setDescription(currentValue); 
     } 
     if(localName.equals("fullbody")) { 
      newsFeedItemList.setFullbody(currentValue); 
     } 
     if(localName.equals("link")) { 
      if(count >= 4) 
       newsFeedItemList.setLink(currentValue); 
     } 
     if(localName.equals("pubDate")) { 
      if(count >= 5) 
       newsFeedItemList.setPubDate(currentValue); 
     } 
     if(localName.equals("image")) { 
      newsFeedItemList.setImage(currentValue); 
     } 

     count++; 
    } 

    @Override 
    public void endDocument() throws SAXException { 
     super.endDocument(); 
    } 


    public NewsFeedItemList getNewsList() { 
     return newsFeedItemList; 
    } 

} 

私は法に過ぎない)(文字でcurrentValue = Html.fromHtml(currentValue).toString();を入れてみました有効になります。また、 "strServerResponseMsg"を送信する前に、私はHTMLに変更しようとしましたが、パーサーは何も解析しませんでした。

私はこれらのトピックを見つけましたが、その解決策は、私のために働いていなかった。 How to strip or escape html tags in Android Display HTML Formatted String

あなたは私を助けることができる場合、私はそんなに感謝しています。ありがとう。

答えて

0

currentValue変数からすべてのHTMLタグを削除するには、次の方法を使用します。

+0

ありがとうございますが、残念ながら動作しません。なぜ私はそれがこのようなものか分かりません:( – Hesam

関連する問題