0

我正在尝试解析以下 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"));
            }

    }   
}
4

1 回答 1

0

加个booleanflaginsideItem怎么样?

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

}
于 2013-04-15T10:21:33.803 回答