2013-04-15 142 views
0

我試圖解析以下XML文件:Android的SAXParser的XML解析

<?xml version="1.0" encoding="utf-8"?> 
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:wfw="http://wellformedweb.org/CommentAPI/" 
         xmlns:slash="http://purl.org/rss/1.0/modules/slash/" ><channel > 
<title>TORi : Hosts Profiles</title> 
<link>http://www.teugadio.com</link> 
<description>TRi : TelOne Rdio Hots Prfiles</description> 
<dc:language>en-us</dc:language> 
<dc:rights>Copyright @copy; 2000-11, teluone.com, All Rights Reserved..</dc:rights> 
<dc:date>2013-04-14T23:07:55+01:00</dc:date> 
<dc:creator>TORi : TeluguOne Radio</dc:creator> 
<dc:subject>TORi : TeluguOne Radio</dc:subject> 
<item > 
<title>Rajesh </title> 
<link>http://www.teluradio.com/rssHstDescr.php?hostId=146</link> 
<guid isPermaLink="false">http://www.telugeradio.com/rssHostDescr.php?hostId=146</guid> 
<pubDate>2013-04-12</pubDate> 
<description>Rajesh</description> 
<media:thumbnail width='66' height='49' url='http://hostphotos/Rajh-Photo.jpg'></media:thumbnail> 
</item> 
<item > 
<title>Prasanthi </title> 
<link>http://www.tuguonadio.com/rssHostDescr.php?hostId=145</link> 
<guid isPermaLink="false">http://www.telugadio.com/rsHostDescr.php?hostId=145</guid> 
<pubDate>2013-04-10</pubDate> 
<description>Prasanthi</description> 
<media:thumbnail width='66' height='49' url='http://hostphotos/Pri-image.jpg'></media:thumbnail> 
</item> 
etc....... 
</channel> 
</rss> 

我能夠分析數據和我的輸出是:

TITLE TORi : Hosts Profiles 
TITLE http://www.teluguoneradio.com 
TITLE TORi : TeluguOne Radio Hosts Profiles 
TITLE en-us 
TITLE Copyright @copy; 2000-11, teluguone.com, All Rights Reserved.. 
TITLE 2013-04-14T23:31:24+01:00 
TITLE TORi : TeluguOne Radio 
TITLETORi : TeluguOne Radio 
TITLE Rajesh 
TITLE Prasanthi 
TITLE Maireyi Ganaaju 
etc.......... 

但是,我想在物品標籤本身中獲取詳細信息?有沒有人告訴我我的代碼應該更改哪些內容?

我所需的輸出是:

TITLE Rajesh 
TITLE Prasanthi 
TITLE Maireyi Ganaaju 
etc.......... 

這裏我的代碼:

import static com.tort.BaseFeedParser.ITEM; 
import static com.tort.BaseFeedParser.LINK; 
import static com.tort.BaseFeedParser.TITLE; 
import static com.tort.BaseFeedParser.PUBDATE; 


import java.util.ArrayList; 
import java.util.List; 

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

public class HostsRssHandler extends DefaultHandler { 
    private List<HostsProfile> messages; 
    private HostsProfile currentMessage; 
    private StringBuilder builder; 

    public List<HostsProfile> getMessages(){ 
     return messages; 
    } 
    @Override 
    public void characters(char[] ch, int start, int length) 
      throws SAXException { 
     super.characters(ch, start, length); 
     builder.append(ch, start, length); 
    } 
    @Override 
    public void endElement(String uri, String localName, String name) 
      throws SAXException { 
     super.endElement(uri, localName, name); 
     if (this.currentMessage != null){ 
      if (localName.equalsIgnoreCase(TITLE)){ 
       currentMessage.setTitle(builder.toString()); 
      } else if (localName.equalsIgnoreCase(LINK)){ 
       currentMessage.setLink(builder.toString()); 
      } 

      else if (localName.equalsIgnoreCase(PUBDATE)){ 
       currentMessage.setDate(builder.toString()); 
      } 


      else if (localName.equalsIgnoreCase(ITEM)){ 
       messages.add(currentMessage); 
      } 
      builder.setLength(0); 
     } 
    } 

    @Override 
    public void startDocument() throws SAXException { 
     super.startDocument(); 
     messages = new ArrayList<HostsProfile>(); 
     builder = new StringBuilder(); 
    } 

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


     if (localName.equalsIgnoreCase(ITEM)){ 
      this.currentMessage = new HostsProfile(); 
     } 
     if (localName.equalsIgnoreCase("thumbnail")) { 
       currentMessage.setMediathumbnail(attributes.getValue("url")); 
      } 

    } 
} 
+0

你是否堅持使用SAX解析器?你似乎沒有龐大的XML文件?我想你可能想看看vtd-xml作爲XML處理的最新技術。 –

回答

0

如何添加boolean標誌insideItem

public class HostsRssHandler extends DefaultHandler { 
    private boolean insideItem; 

    @Override 
    public void endElement(String uri, String localName, String name) 
     throws SAXException { 
    super.endElement(uri, localName, name); 
    if (this.currentMessage != null && insideItem){ 
     if (localName.equalsIgnoreCase(TITLE)){ 
     currentMessage.setTitle(builder.toString()); 
     } else if (localName.equalsIgnoreCase(LINK)){ 
     currentMessage.setLink(builder.toString()); 
     } else if (localName.equalsIgnoreCase(PUBDATE)){ 
     currentMessage.setDate(builder.toString()); 
     } else if (localName.equalsIgnoreCase(ITEM)){ 
     messages.add(currentMessage); 
     insideItem = false; 
     } 
     builder.setLength(0); 
    } 
    } 

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


    if (localName.equalsIgnoreCase(ITEM)){ 
     this.currentMessage = new HostsProfile(); 
     insideItem = true; 
    } else if (localName.equalsIgnoreCase("thumbnail") && insideItem) { 
     currentMessage.setMediathumbnail(attributes.getValue("url")); 
    } 
    } 

}