1

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

問題はおそらく文字セットにありますが、これを実装する方法がわかりませんでした。どんな助けや指導も非常に感謝しています。

よろしくお願いいたします。

バイバブ・ゴスワミ

4

3 に答える 3

1

私もシンジケーションを使用しており、公開日とタイトルを取得できます。

私のコードは次のとおりです。

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

これは機能し、XML が得られるという理由だけで Bloomberg Url を使用しました。

あなたのクエリが何か他のものだった場合は、私に知らせてください:)

于 2012-03-19T06:01:16.550 に答える
0

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();
      }
于 2012-05-04T02:57:27.120 に答える
0

これは、バイト オーダー マークの問題によるものです。問題と修正を示す 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());
        }
    }

}
于 2013-01-28T08:10:50.940 に答える