Question

I've done XML parsing via XmlPullParser and it works fine until I add special characters to my xml file such as š,č,ř,ž and so on. It replaces the character with "?" sign. Is there any way how to get rid of that?

Here is my Xml parser class:

public class ClubsXmlPullParser {

    static final String CLUB = "club";
    static final String NAME = "name";
    static final String ABOUT = "about";
    static final String STADIUM = "stadium";
    static final String LOGO = "logo";
    static final String MOREABOUT = "moreAbout";

    public static List<LeagueClub> getItemsFromFile(Context ctx) {


        List<LeagueClub> clubs;
        clubs = new ArrayList<LeagueClub>();


        LeagueClub curItem = null;

        String curText = "";

        try {

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xpp = factory.newPullParser();


            FileInputStream fis = ctx.openFileInput("clubs.xml");
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));


            xpp.setInput(reader);


            int eventType = xpp.getEventType();


            while (eventType != XmlPullParser.END_DOCUMENT) {

                String tagname = xpp.getName();


                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase(CLUB)) {
                        curItem = new LeagueClub();
                    }

                    break;

                case XmlPullParser.TEXT:

                    curText = xpp.getText();
                    break;

                case XmlPullParser.END_TAG:
                    if (tagname.equalsIgnoreCase(CLUB)) {

                        clubs.add(curItem);
                    } else if (tagname.equalsIgnoreCase(NAME)) {

                        curItem.setName(curText);
                    } else if (tagname.equalsIgnoreCase(ABOUT)) {

                        curItem.setAbout(curText);
                    } else if (tagname.equalsIgnoreCase(STADIUM)) {

                        curItem.setStadium(curText);
                    } else if (tagname.equalsIgnoreCase(LOGO)) {

                        curItem.setLogo(curText);
                    } else if (tagname.equalsIgnoreCase(MOREABOUT)) {

                        curItem.setName(curText);
                    } 



                    break;

                default:
                    break;
                }

                eventType = xpp.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return clubs;
    }

}
Was it helpful?

Solution

Assuming that you know how to obtain the encoding of the file, you can use the alternative version of setInput - setInput(InputStream inputStream, String inputEncoding)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top