prof18 / RSS-Parser

A Kotlin Multiplatform library to parse a RSS Feed
Apache License 2.0
499 stars 127 forks source link

RSS feed starting with blank lines - XmlPullParserException: Unexpected token #76

Closed ruanmed closed 3 years ago

ruanmed commented 3 years ago

Describe the bug When parsing an invalid XML RSS feed which starts with a newline the library throws an error.

org.xmlpull.v1.XmlPullParserException: Unexpected token (poisition:uknown @2:1 in java.io.InputStreamReader@f20908e)

My class:

public class MainActivity extends AppCompatActivity {
    private final String RSS_FEED = "https://www.cnj.jus.br/tag/inteligencia-artificial/feed/";

    ListView conteudoRSS;
    List<Article> noticias;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        conteudoRSS = findViewById(R.id.conteudoRSS);
    }

    @Override
    protected void onStart() {
        super.onStart();

        Parser p = new Parser.Builder().build();
        p.onFinish(
                new OnTaskCompleted() {
                    @Override
                    public void onTaskCompleted(Channel channel) {
                        noticias = channel.getArticles();
                        runOnUiThread(
                                () -> {
                                    RssAdapter adapter = new RssAdapter(
                                            getApplicationContext(),
                                            noticias
                                    );
                                    conteudoRSS.setAdapter(adapter);
                                }
                        );

                    }

                    @Override
                    public void onError(Exception e) {
                        Log.e("RSS_APP",e.getMessage());
                    }
                }
        );
        p.execute(RSS_FEED);
}

The link of the RSS Feed https://www.cnj.jus.br/tag/inteligencia-artificial/feed/

Solution I've actually found a solution to this problem, on com.prof.rssparser/Parser.kt file just trimming the rssFeed string before sending it do the XML parser does the job:

                val f2 = service.submit(XMLParser(rssFeed))

becomes

                val f2 = service.submit(XMLParser(rssFeed.trim()))

and I'm pretty sure it does not have almost any performance impact on the normal flow of the app.

A second option to fix this issue could be to add an option/parameter in the Parser class to do the trim on the rssFeed before parsing?

prof18 commented 3 years ago

Hi, I've fixed the issue by trimming the feed before parsing. That actually makes sense to do it. I'll make a new release with the fix soon Thanks for the suggestion