我遇到了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
}
这项工作到目前为止。注释编码打印出我想要的内容,即打印我存储在链表中的项目列表。
我的主要目标是将所有这些项目放在我已经无序链接列表中做得对。但我的问题是如何让用户访问应用程序类中的此列表?例如,我希望能够向用户显示从列表中删除项目的选项。我知道链接列表的方法以及如何创建GUI但是要清楚,我不知道如何从应用程序类访问此列表。或者我在错误的地方创建LinkedList?
感谢
答案 0 :(得分:0)
您需要将RSSParser移动为Project4类中的变量,并提供访问它的方法。您的静态main方法应该实例化Project4的实例并创建一个新的RSSParser()。使用该实例解析订阅源并创建一个getter方法以从GUI获取对它的访问权限。
例如,像这样:
public class Project4 {
private parser RSSParser;
public RSSParser getParser() {
return parser
}
public static void main (String [] args) throws IOException, SAXException {
Project4 p = new Project4();
XMLReader read = XMLReaderFactory.createXMLReader();
p.parser= new RSSParser();
read.setContentHandler(p.parser);
read.parse("http://feeds.ign.com/ign/games-articles");
/// loop here and connect up your GUI
}
}
另外,您可能需要查看ROME项目以解析RSS源。