Which is the best ?xml parser for android or Regex

2019-09-17 13:01发布

问题:

I want to strip out the tags from this page http://ptwc.weather.gov/feeds/ptwc_rss_indian.xml

Just I want to take the content between description tags in the above xml

a)For this whether I can use xml parser or regex

b) If I use regex how to strip out that? (I used the below code but it's not working

Pattern p= Pattern.compile("<description>(.*)</description>",Pattern.DOTALL);
        //Matcher matcher=p.matcher(result);
        Matcher matcher = p.matcher(result);

        if (matcher.matches())
        {
            String s1=matcher.group(1);
            textView.setText(s1);
        }
        else
        {
            textView.setText("No Matches);
        }

回答1:

You should always use an XML parser. It is much safer and in the end easier to implement. An expression that might work in your tests may break with a nested tag or a minor change in the structure. Also, XML is much more maintainable should you want to retrieve more data from the same files.

Use an XML Parser!

Edit: read an interesting discussion



回答2:

I agree with Aleadam in that you should always use an XML Parser but I also believe that an XML parser should not be a hassle to write or use which is why I always recommend using the Simple XML Framework on Android.

So much so that I even wrote a blog post on how to use Simple in Android.