【问题标题】:While parsing RSS feed through Rome getting Content is not allowed in prolog在prolog中不允许通过罗马解析RSS提要获取内容
【发布时间】:2011-12-12 11:11:25
【问题描述】:

使用 Rome API 解析 RSS 提要我收到此错误:

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

代码如下:

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();
    }
}

我检查了一些链接,例如:

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

问题可能出在字符集上,但我想不出一种方法来实现它。 任何帮助或指导将不胜感激。

感谢和问候,

外婆哥斯瓦米

【问题讨论】:

  • 我尝试通过 jakarta feed 解析器实现我的功能,并且可以解析这个 URL。与 RSS 相比,我认为 Jakarta Feed 解析器可以处理更多类型的 Feed。

标签: java character-encoding syndication rome


【解决方案1】:

我也在使用 Syndication,我可以获得发布日期和标题。

我的代码如下:

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")
}

这行得通,我使用 Bloomberg Url 只是因为它给了我一个 XML。

如果您的查询是其他问题,请告诉我:)

【讨论】:

    【解决方案2】:

    您可以使用 SyndFeedSyndEntry 来解析 xml

    还需要检查xml是否有效

    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】:

      这是由于Byte Order Mark problem。这是一个演示问题和修复的 JUnit 测试用例:

      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());
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-23
        • 2016-11-28
        • 1970-01-01
        • 2020-01-16
        • 1970-01-01
        • 2011-06-01
        • 2011-07-23
        • 2011-06-19
        相关资源
        最近更新 更多