【问题标题】:XmlPullParser view special alphabetical charactersXmlPullParser 查看特殊字母字符
【发布时间】:2014-03-23 00:34:42
【问题描述】:

我已经通过 XmlPullParser 完成了 XML 解析,它工作正常,直到我向我的 xml 文件中添加特殊字符,例如 š、č、ř、ž 等。它将字符替换为“?”标志。有什么办法可以摆脱吗?

这是我的 Xml 解析器类:

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

}

【问题讨论】:

    标签: android xml xml-parsing android-xml xmlpullparser


    【解决方案1】:

    假设你知道如何获取文件的编码,你可以使用setInput的替代版本-setInput(InputStream inputStream, String inputEncoding)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-05
      • 2010-11-16
      • 2022-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      相关资源
      最近更新 更多