我已经通过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;
}
}
答案 0 :(得分:0)
假设您知道如何获取文件的编码,则可以使用替代版本的setInput
- setInput(InputStream inputStream, String inputEncoding)