获取整个事件而不是Java中的一部分

时间:2015-11-28 21:46:36

标签: java rss

其实我有两个问题。其中一个是我的RSS阅读器中有空字段。我得到: screenshot 我试图找到我的代码有什么问题,我注意到这个方法应该单独读取标题,描述和其他东西,它们将它们全部读出来。我的意思是我的String应该只有像title这样的单个对象,它们总是像:screenshot 我不明白为什么。你能帮助我吗?我知道有很多代码,但问题出在RSSFeedParser类的reedFeed中。

主:

import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.util.Scanner;

/**
 * Created by Maciek on 2015-11-12.
 */
public class ReadTest {
    private static Scanner scanner = new Scanner(System.in);
    private static RSSList rssList = new RSSList();
    private static String url;
    public static String righturl;

public static void main(String[] args) throws XMLStreamException, IOException, ClassNotFoundException {
    MENU();
}

public static void MENU() throws IOException, ClassNotFoundException, XMLStreamException {
    int choice;
    System.out.println("MENU:");
    System.out.println(" 1 = Wydrukuj RSS");
    System.out.println(" 2 = Pokaz liste dodanych RSS");
    System.out.println(" 3 = Dodaj link RSS");
    System.out.println(" 4 = Usun link RSS");
    System.out.println(" 5 = Wydrukuj historie");
    System.out.println("5 = zakoncz");

    choice = scanner.nextInt();
    switch (choice) {
        case 1:
            rssList.readCurrentlyList();
            printRSS();
            MENU();
            break;
        case 2:
            rssList.readCurrentlyList();
            printList();
            MENU();
            break;
        case 3:
            rssList.readCurrentlyList();
            rssList.readHistory();
            feedURL();
            checkCurrentyList();
            rssList.saveCurrentlyList();
            MENU();
            break;
        case 4:
            rssList.readCurrentlyList();
            System.out.println("Ktory link chcesz usunac?");
            printList();
            deleteIndex();
            rssList.saveCurrentlyList();
            MENU();
            break;
        case 5:
            rssList.readHistory();
            readHistory();
            MENU();
            break;
    }
}

public static void printRSS() throws XMLStreamException {
    for (int i = 0; i < rssList.RSSList.size(); i++) {
        righturl = rssList.RSSList.get(i);
        RSSFeedParser parser = new RSSFeedParser(righturl);
        Feed feed = parser.readFeed();
        System.out.println(feed);
        for (FeedMessage message : feed.getMessages()) {
            System.out.println(message);
        }
    }
}
public static void printList(){
    for(int i = 0; i<rssList.RSSList.size(); i++){
        System.out.println(i+1 +". "+rssList.RSSList.get(i));
    }
}
public static void deleteIndex(){
    int index = scanner.nextInt();
    rssList.RSSList.remove(index-1);
}
public static void feedURL(){
    scanner.nextLine();
    url = scanner.nextLine();
}
public static void addToHistory(){
    boolean thereAlreadyIs = false;
    for(int i = 0; i<rssList.RSSHistory.size(); i++){
        if(rssList.RSSHistory.get(i).equals(url)){
            thereAlreadyIs = true;
        }
    }
    if(thereAlreadyIs==false){
        rssList.RSSHistory.add(url);
    }
}
public static void checkCurrentyList() throws IOException, ClassNotFoundException {
    boolean thereAlreadyIs = false;
    rssList.readCurrentlyList();
    for (int i = 0; i < rssList.RSSList.size(); i++) {
        if (url.equals(rssList.RSSList.get(i))) {
            thereAlreadyIs = true;
        }
    }
    if (thereAlreadyIs == true) {
        System.out.println("Juz jest dodany RSS o takim adresie!");
    }
    else{
        addRSS();
    }
}
public static void addRSS(){
    rssList.RSSList.add(url);
    addToHistory();
}
public static void readHistory() throws IOException, ClassNotFoundException {
    rssList.readHistory();
    for(int i =0; i<rssList.RSSHistory.size(); i++){
        System.out.println("1. "+rssList.RSSHistory.get(i));
    }
}
}

收集读者数据的类。

import java.util.ArrayList;

import java.util.List;

/**
 * Created by Maciek on 2015-11-11.
 */

public class Feed {

final String title;
final String description;
final String link;
final String language;
final String copyright;
final String pubDate;

final List<FeedMessage> entries = new ArrayList<FeedMessage>();

public Feed(String title, String description, String link, String language, String copyright, String pubDate){
    this.title=title;
    this.description=description;
    this.link=link;
    this.language=language;
    this.copyright=copyright;
    this.pubDate=pubDate;
}
public List<FeedMessage> getMessages(){
    return entries;
}
public String getTitle(){
    return title;
}
public String getDescription(){
    return description;
}
public String getLink(){
    return link;
}
public String getLanguage(){
    return language;
}
public String getCopyright(){
    return copyright;
}
public String getPubDate(){
    return pubDate;
}
public String toString(){
    return "Freed: [Title: " +title+", Description: "+description+", Copyright: "+copyright+", Language: "+language+", PubDate: "+pubDate;
}
}

用于打印RSS的类

public class FeedMessage {
String title;
String description;
String link;
String author;
String guid;

public void setTitle(String title){
    this.title=title;
}
public String getTitle(){
    return title;
}
public void setDescription(String description){
    this.description=description;
}
public String getDescription(){
    return description;
}
public void setLink(String link){
    this.link=link;
}
public String getLink(){
    return link;
}
public void setAuthor(String author){
    this.author=author;
}
public String getAuthor(){
    return author;
}
public void setGuid(String guid){
    this.guid=guid;
}
public String getGuid(){
    return guid;
}
public String toString(){
    return "FeedMessage: [title= "+title+", Description= "+description+", Link= "+link+", Author: "+author+", Guid= "+guid;
}
}

FeedParser类:

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.XMLEvent;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * Created by Maciek on 2015-11-15.
 */
public class RSSFeedParser {
    public static final String ITEM = "item";
    public static String description = "";
    public static String title = "";
    public static String link = "";
    public static String language = "";
    public static String copyright = "";
    public static String author = "";
    public static String pubDate = "";
    public static String guid = "";
    public Feed feed;
    public boolean isFeedHeader;
public InputStream in;
public XMLInputFactory inputFactory = XMLInputFactory.newInstance();
public XMLEventReader eventReader;
public  XMLEvent event;
final URL url;
TITLE tytul = new TITLE();
AUTHOR autor =  new AUTHOR();
COPYRIGHT prawa = new COPYRIGHT();
DESCRIPTION opis = new DESCRIPTION();
GUID identyfikacja = new GUID();
LANGUAGE jezyk = new LANGUAGE();
LINK linskon = new LINK();
PUB_DATE publikajca = new PUB_DATE();
ITEM itemson = new ITEM();

public RSSFeedParser(String feedUrl) {
    try {
        url = new URL(feedUrl);
        in = read();
        eventReader = inputFactory.createXMLEventReader(in);
        event = eventReader.nextEvent();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

public Feed readFeed() throws XMLStreamException {
    try {
        isFeedHeader = true;
        event = eventReader.nextEvent();
        while(eventReader.hasNext()) {
            itemson.feedData(this);
            tytul.feedData(this);
            opis.feedData(this);
            System.out.println(description);
            linskon.feedData(this);
            identyfikacja.feedData(this);
            jezyk.feedData(this);
            autor.feedData(this);
            publikajca.feedData(this);
            prawa.feedData(this);
            eventReader.close();
        }


    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
    return feed;
}

public Feed setAllElements() throws XMLStreamException {
    FeedMessage message = new FeedMessage();
    message.setAuthor(author);
    message.setDescription(description);
    message.setGuid(guid);
    message.setLink(link);
    message.setTitle(title);
    feed.getMessages().add(message);
    return feed;
}


private InputStream read() throws XMLStreamException {
    try {
        return url.openStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

public String getCharacterData(XMLEvent event, XMLEventReader eventReader) throws XMLStreamException {
    String results = "";
    if (event instanceof Characters) {
        results = event.asCharacters().getData();
    }
    return results;
}
}

我将在这里粘贴一个在以下后面实现的类:

public interface Case {
    void feedData(RSSFeedParser rss) throws XMLStreamException;
}

重要的课程是ITEM:

public class ITEM implements Case {


@Override
public void feedData(RSSFeedParser rss) throws XMLStreamException {
    if (rss.isFeedHeader) {
        rss.isFeedHeader = false;
        rss.feed = new Feed(rss.title, rss.link, rss.description, rss.language, rss.copyright, rss.pubDate);
    }
    rss.event = rss.eventReader.nextEvent();

    if (rss.event.isEndElement()) {
        if (rss.event.asEndElement().getName().getLocalPart() == (rss.ITEM)) {
            try {
                rss.setAllElements();
            } catch (XMLStreamException e) {
                e.printStackTrace();
            }
        }
    }
}

}

正如我所说的其中一个类: 例如标题:

public class TITLE  implements Case {

@Override
public void feedData(RSSFeedParser rss) throws XMLStreamException {
    rss.title = rss.getCharacterData(rss.event, rss.eventReader);
    rss.eventReader.close();
}

}

非常感谢你的每一次帮助!

0 个答案:

没有答案