While parsing RSS feed through Rome getting Content is not allowed in prolog

岁酱吖の 提交于 2019-12-08 07:08:59

问题


Using Rome API to parse the RSS feeds I am getting this error :

com.sun.syndication.io.ParsingFeedException: Invalid XML
    at com.sun.syndication.io.WireFeedInput.build(WireFeedInput.java:210)

The code is as below:

public static void main(String[] args) {
    URL url;
    XmlReader reader = null;
    SyndFeed feed; 

    try {
        url = new URL("https://www.democracynow.org/podcast.xml");
        reader = new XmlReader(url);
        feed = new SyndFeedInput().build(reader);
        for (Iterator<SyndEntry> i =feed.getEntries().iterator(); i.hasNext();) {
            SyndEntry entry = i.next();
            System.out.println(entry.getPublishedDate()+" Title  "+entry.getTitle());

        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

I checked for some of the links like :

http://old.nabble.com/Invalid-XML:-Error-on-line-1:-Content-is-not-allowed-in-prolog.-td21258868.html

Where the problem is presumably is of charsets but I could not figure a way to get this implemented. Any help or guidance would be highly appreciative.

Thanks and Regards,

Vaibhav Goswami


回答1:


I am using Syndication as well and i am able to get published date and title.

My code is as follows:

URL feedUrl = new URL("http://www.bloomberg.com/tvradio/podcast/cat_markets.xml");

SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));

for (Iterator i = feed.getEntries().iterator(); i.hasNext();)
{
SyndEntry entry = (SyndEntry) i.next();
System.out.println("title |"+entry.getTitle()+"   " -timeStamp "+entry.getPublishedDate()"\n")
}

This works , and i have used Bloomberg Url just cause it gives me a XML.

If your query was something else , do let me know :)




回答2:


you can use SyndFeed and SyndEntry for parsing the xml

Also you need to check whether the xml is a valid one

URL url  = new URL("http://feeds.feedburner.com/javatipsfeed");
    XmlReader reader = null;
    try {
      reader = new XmlReader(url);
      SyndFeed feeder = new SyndFeedInput().build(reader);
      System.out.println("Feed Title: "+ feeder.getAuthor());
      for (Iterator i = feeder.getEntries().iterator(); i.hasNext();) {
        SyndEntry syndEntry = (SyndEntry) i.next();
        System.out.println(syndEntry.getTitle());
      }
      } finally {
            if (reader != null)
                reader.close();
      }



回答3:


It's due to a Byte Order Mark problem. Here is a JUnit test case that demonstrates the problem and the fix:

package rss;

import org.xml.sax.InputSource;

import java.io.*;
import java.net.*;

import com.sun.syndication.io.*;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BOMInputStream;
import org.junit.Test;

public class RssEncodingTest {

    String url = "http://www.moneydj.com/KMDJ/RssCenter.aspx?svc=NH&fno=1&arg=X0000000";

    // This works because we use InputSource direct from the UrlConnection's InputStream

    @Test
    public void test01() throws MalformedURLException, IOException,
            IllegalArgumentException, FeedException {
        try (InputStream is = new URL(url).openConnection().getInputStream()) {
            InputSource source = new InputSource(is);
            System.out.println("description: "
                    + new SyndFeedInput().build(source).getDescription());
        }
    }

    // But a String input fails because the byte order mark problem

    @Test
    public void test02() throws MalformedURLException, IOException,
            IllegalArgumentException, FeedException {
        String html = IOUtils.toString(new URL(url).openConnection()
                .getInputStream());
        Reader reader = new StringReader(html);
        System.out.println("description: "
                + new SyndFeedInput().build(reader).getDescription());
    }

    // We can use Apache Commons IO to fix the byte order mark

    @Test
    public void test03() throws MalformedURLException, IOException,
            IllegalArgumentException, FeedException {
        String html = IOUtils.toString(new URL(url).openConnection()
                .getInputStream());
        try (BOMInputStream bomIn = new BOMInputStream(
                IOUtils.toInputStream(html))) {
            String f = IOUtils.toString(bomIn);
            Reader reader = new StringReader(f);
            System.out.println("description: "
                    + new SyndFeedInput().build(reader).getDescription());
        }
    }

}


来源:https://stackoverflow.com/questions/8473410/while-parsing-rss-feed-through-rome-getting-content-is-not-allowed-in-prolog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!