2012-05-04 4 views
1

나는 안드로이드 애플 리케이션을 구축하고 있으며 구문 분석하려고하고 RSS 피드에서이 설명 태그를 구문 분석하려고합니다.안드로이드에서 RSS 피드 설명을 구문 분석

<description> 
<![CDATA[ 
<h1>What Do You Worship?</h1><p>This week <a href="https://twitter.com/#!/nathanielcoon">@Nathanielcoon</a> teaches on the life of Abraham.</a></p> <p><a href="http://www.esvbible.org/search/Genesis+12%3A7-8/">Genesis 12:7-8</a></p> <p><a href="http://www.esvbible.org/search/Genesis+13%3A2-4/">Genes 
]]> 
<![CDATA[ 
is 13:2-4</a></p> <p><a href="http://www.esvbible.org/search/Genesis+13%3A14-18/">Genesis 13:14-18</a></p> <p><a href="http://www.esvbible.org/search/Genesis+14%3A17-24/">Genesis 14:17-24</a></p> <p><a href="http://www.esvbible.org/search/Genesis+15%3A1-21/">Genesis 15:1-21</a></p> <p><a href="http://www.esvbible.org/search/Genesis+17%3A3/">Genesis 17:3</a></p> <p><a href="http://www.esvbible.org/search/Genesis+17%3A17/">Genesis 17:17</a></p> <p><a href="http://www.esvbible.org/search/Genesis+18%3A1-33/">Genesis 18:1-33</a></p> <p><a href="http://www.esvbible.org/search/Genesis+21%3A33/">Genesis 21:33</a></p> <p><a href="http://www.esvbible.org/search/Genesis+22%3A4-14/">Genesis 22:4-14</a></p> <p><a href="http://www.esvbible.org/search/Isaiah+44%3A1-20/">Isaiah 44:1-20</a></p> <p><a href="http://www.esvbible.org/search/Revelation+4%3A1-5%3A15/">Revelation 4:1-5:14</a></p> <p><a href="http://www.esvbible.org/search/Isaiah+45%3A20/">Isaiah 45:20</a></p> <p><a href="http://www.esvbible.org/search/Isaiah+45%3A20-22/">Isaiah 45:20-22</a></p> <p><a href="http://www.esvbible.org/search/Matthew+27%3A50-51/">Matthew 27:50-51</a></p> <p><a href="http://www.esvbible.org/search/Mark+15%3A37-38/">Mark 15:37-38</a></p> <p><a href="http://www.esvbible.org/search/Luke+23%3A45-46/">Luke 23:45-46</a></p> <p><a href="http://www.esvbible.org/search/Hebrews+10%3A19/">Hebrews 10:19</a></p> <p><a href="http://www.esvbible.org/search/Isaiah+6%3A1-13/">Isaiah 6:1-13</a></p> <p>For more resources or to contact us visit <a href="http://www.refugecf.com/resources/">RefugeCf.com</a></p> <p>Follow <a href="https://twitter.com/RefugeCF"> @RefugeCF</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script> on Twitter.</p> 

그러나 처음 두 줄만 표시됩니다.

<h1>What Do You Worship?</h1><p>This week <a href="https://twitter.com/#!/nathanielcoon">@Nathanielcoon</a> teaches on the life of Abraham.</a></p> 

내가 찾지 못한 스위치가 있습니까? 내 Android 앱에 SAXParser를 사용하고 있습니다.

도 있습니다. 내가 파싱/아이튠즈를 얻으려고 : 요약 태그, 전혀 작동하지 않았다. 왜 그런지 궁금합니다.

감사합니다.

편집 : 여기에 내 RSSHandler 코드가 있습니다. 아프지 만 게시해야하는 경우이 문제를 해결할 것입니다. 나머지는 활동에 보내고보고있는 것입니다. (내가 웹뷰에서보고있어.)

import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 

public class RSSHandler extends DefaultHandler { 

    final int state_unknown = 0; 
    final int state_title = 1; 
    final int state_description = 2; 
    final int state_link = 3; 
    final int state_pubdate = 4; 
    final int state_guid = 5; 
    final int state_subtitle = 6; 
    int currentState = state_unknown; 

    RSSFeed feed; 
    RSSItem item; 

    boolean itemFound = false; 

    RSSHandler(){ 
    } 

    RSSFeed getFeed(){ 
     return feed; 
    } 

    @Override 
    public void startDocument() throws SAXException { 
     // TODO Auto-generated method stub 
     feed = new RSSFeed(); 
     item = new RSSItem(); 

    } 

    @Override 
    public void endDocument() throws SAXException { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void startElement(String uri, String localName, String qName, 
      Attributes attributes) throws SAXException { 
     // TODO Auto-generated method stub 

     if (localName.equalsIgnoreCase("item")){ 
      itemFound = true; 
      item = new RSSItem(); 
      currentState = state_unknown; 
     } 
     else if (localName.equalsIgnoreCase("title")){ 
      currentState = state_title; 
     } 
     else if (localName.equalsIgnoreCase("subtitle")){ 
      currentState = state_subtitle; 
     } 
     else if (localName.equalsIgnoreCase("description")){ 
      currentState = state_description; 
     } 
     else if (localName.equalsIgnoreCase("link")){ 
      currentState = state_link; 
     } 
     else if (localName.equalsIgnoreCase("pubdate")){ 
      currentState = state_pubdate; 
     } 
     else if (localName.equalsIgnoreCase("guid")){ 
      currentState = state_guid; 
     } 
     else{ 
      currentState = state_unknown; 
     } 

    } 

    @Override 
    public void endElement(String uri, String localName, String qName) 
      throws SAXException { 
     // TODO Auto-generated method stub 
     if (localName.equalsIgnoreCase("item")){ 
      feed.addItem(item); 
     } 
    } 

    @Override 
    public void characters(char[] ch, int start, int length) 
      throws SAXException { 
     // TODO Auto-generated method stub 

     String strCharacters = new String(ch,start,length); 

     if (itemFound==true){ 
     // "item" tag found, it's item's parameter 
      switch(currentState){ 
      case state_title: 
       item.setTitle(strCharacters); 
       break; 
      case state_subtitle: 
       item.setsubTitle(strCharacters); 
       break; 
      case state_description: 
       item.setDescription(strCharacters); 
       break; 
      case state_link: 
       item.setLink(strCharacters); 
       break; 
      case state_pubdate: 
       item.setPubdate(strCharacters); 
       break; 
      case state_guid: 
       item.setGuid(strCharacters); 
       break; 
      default: 
       break; 
      } 
     } 
     else{ 
     // not "item" tag found, it's feed's parameter 
      switch(currentState){ 
      case state_title: 
       feed.setTitle(strCharacters); 
       break; 
      case state_subtitle: 
       feed.setsubTitle(strCharacters); 
       break; 
      case state_description: 
       feed.setDescription(strCharacters); 
       break; 
      case state_link: 
       feed.setLink(strCharacters); 
       break; 
      case state_pubdate: 
       feed.setPubdate(strCharacters); 
       break; 
      case state_guid: 
       feed.setGuid(strCharacters); 
       break; 
      default: 
       break; 
      } 
     } 

     currentState = state_unknown; 
    } 


} 
+0

코드를 게시하십시오. – FoamyGuy

+0

게시 됨, 감사합니다. – sonicemerald

답변

0

나는 당신이 CDATA에 [and]가 없다고 생각하니?

http://java.sun.com/webservices/reference/tutorials/jaxp/html/sax.html

SAX 파서 나는 그 방법에 중단 점을 추가하고 당신이 무엇을 기대하고있어 경우에 볼 것, 조금 이해하기 어려울 수 있습니다.

+0

그 링크에서 [및]을 사용할 수 없지만 두 번째 절반은 첫 번째 CDATA 만 가져 오기 때문에 나타나지 않습니다. 그렇다면 두 CDATA를 어떻게 결합 할 수 있습니까? 편집 : startCDATA(), endCDATA() 찾을 수 있지만 어떻게 그들을 사용할 수 있을지 잘 모르겠습니다. – sonicemerald

+0

문서에 오타가있는 것처럼 보입니다. 괜찮을 것입니다. – NoBugs

+0

흠 .. 방금 중단 점에 대한 여러분의 의견을 보았습니다. 다소 다른 관련 질문 : 활동을 열기 전에 saxparser가 모든 것을 구문 분석합니까? 서비스가 충분하지 않으면 활동이 안드로이드가 응답하지 않는다고 생각하는 지점까지로드하는 데 시간이 걸릴 것으로 나타났습니다. – sonicemerald

관련 문제