嗯,我遇到了RSSParsing的另一个问题。
这是我迄今为止所做的。
我已经成功地从我想要的RSSFeed中访问数据。
我已经将信息存储在一个Item类对象中,没有任何问题(据我所知)。这门课采用标题,说明,链接,出版日期。如何在Java中存储和处理来自RSS Feed的检索信息?
public class Item {
private String link;
private String title;
private String pubDate;
private String description;
// Default Constructor
public Item()
{
link = new String();
title = new String();
pubDate = new String();
description = new String();
}
// Parameterized Constructor
public Item(String iTitle, String iDescription, String iLink, String iPubDate)
{
link = iLink;
title = iTitle;
pubDate = iPubDate;
description = iDescription;
}
public void setDate(String newPubDate)
{
pubDate = newPubDate;
}
public void setLink(String newLink)
{
link = newLink;
}
public void setTitle(String newTitle)
{
title = newTitle;
}
public void setDescription(String newDescription)
{
description = newDescription;
}
public String getDate()
{
return pubDate;
}
public String getLink()
{
return link;
}
public String getTitle()
{
return title;
}
public String getDescription()
{
return description;
}
public String toString()
{
return "Title: " + title + "\n" + "Publication Date: " + pubDate + "\n" + "Description: " + description + "\n" + "Link: " + link;
}
以下是我RSSParser处理类
import org.xml.sax.Attributes;
import lists.LinkedUnorderedList;
import org.xml.sax.helpers.DefaultHandler;
public class RSSParser extends DefaultHandler{
int itemCount = 0;
boolean item = false;
boolean link = false;
boolean title = false;
boolean pubDate = false;
boolean description = false;
Item theItem = new Item();
String itemPubDate = new String();
LinkedUnorderedList<Item> theUnorderedList = new LinkedUnorderedList();
public void startDocument()
{
System.out.println("Starts Parsing the Document.....\n");
}
public void endDocument()
{
System.out.println(theUnorderedList + "\n\n" + itemCount);
System.out.println("\nEnds parsing Document...");
}
public void startElement(String nameSpaceURI, String localName, String qName, Attributes atts)
{
if (qName.equalsIgnoreCase("item"))
{
theUnorderedList.addToRear(theItem);
theItem = new Item();
itemCount++;
}
else if (qName.equalsIgnoreCase("link"))
{
link = true;
}
else if (qName.equalsIgnoreCase("title"))
{
title = true;
}
else if (qName.equalsIgnoreCase("pubDate"))
{
pubDate = true;
}
else if (qName.equalsIgnoreCase("description"))
{
description= true;
}
}
public void endElement(String nameSpaceURI, String localName, String qName, Attributes atts)
{
System.out.print("End Element: " + qName);
}
public void characters(char[] ch, int start, int length)
{
if (title)
{
//System.out.println("Title: " + new String(ch, start, length));
theItem.setTitle(new String(ch, start, length));
title = false;
}
else if (link)
{
//System.out.println("Link: " + new String(ch, start, length));
theItem.setLink(new String(ch, start, length));
link = false;
}
else if (description)
{
//System.out.println("Description: " + new String(ch, start, length));
theItem.setDescription(new String(ch, start, length));
description = false;
}
else if (pubDate)
{
itemPubDate = new String(ch, start, length);
//System.out.println("PubDate: " + itemPubDate + "\nItemCount: " + itemCount + "\n\n");
theItem.setDate(new String(ch, start, length));
pubDate = false;
}
} // ends characters Method
最后是我的应用程序类
//import java.net.URL;
//import java.util.Scanner;
import java.io.IOException;
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
//import java.io.InputStreamReader;
import org.xml.sax.helpers.XMLReaderFactory;
public class Project4 {
public static void main (String [] args) throws IOException, SAXException
{
XMLReader read = XMLReaderFactory.createXMLReader();
RSSParser parser= new RSSParser();
read.setContentHandler(parser);
read.parse("http://feeds.ign.com/ign/games-articles");
} // Ends main
}
此功能到目前为止。评论编码打印出我想要的,这是打印我已经存储在链接列表中的项目列表。
我的主要目标是将所有这些项目放在UnorderedLinkedList中,这是我正确完成的。但我的问题是如何让用户访问应用程序类中的这个列表?例如,我希望能够向用户提供有关从列表中删除项目的选项。我知道链表的方法,以及如何创建一个GUI,但要清楚,我不知道如何从应用程序类访问这个列表。或者我在错误的地方创建LinkedList?
谢谢
谢谢!!!!!非常!!!一直试图完成这个几天。是的,我研究过使用ROME,但我无法理解它(不要笑)。肯定会再看一遍,但我主要关心的是能够使用Java的默认库来处理这个项目。 (SAX或DOM)。 – Raw415 2013-05-09 20:37:26
不客气:) – 2013-05-09 23:33:11