【发布时间】: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