好吧,我遇到了 RSSParsing 的另一个问题。
这是我到目前为止所做的。
我已经成功地访问了我想要的 RSSFeed 中的数据。
我已经将其中的信息毫无问题地存储到了一个 Item 类对象中(据我所知)。此类包含标题、描述、链接、发布日期。
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?
谢谢