我解析了一些RSS提要(嘗試不同的...),每次都非常隨機的字符被刪減。 我在做什麼錯?它爲什麼在某些情況下有效,而在其他情況下則不起作用? 還有另一種方法嗎? XML(大多數情況下)將包含UTF-8字符(如ä,ö,ü等),因此解決方案也應該與這些字符一起使用。解析一個RSS提要削減字符
如果您需要更多信息(更多代碼,更多詳細信息等),請讓我知道!
這裏是我的代碼:
public class RSSHandler extends DefaultHandler {
final int state_unknown = 0;
final int state_title = 1;
final int state_description = 2;
final int state_link = 3;
final int state_pubdate = 4;
int currentState = state_unknown;
StringBuilder strCharacters;
RSSFeed feed;
RSSItem item;
boolean inEntity = false;
String entityName = "";
boolean itemFound = false;
public RSSHandler() {
strCharacters = new StringBuilder();
}
public RSSFeed getFeed() {
return feed;
}
@Override
public void startDocument() throws SAXException {
feed = new RSSFeed();
item = new RSSItem();
}
@Override
public void endDocument() throws SAXException {
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
strCharacters = new StringBuilder();
if (localName.equalsIgnoreCase("item")) {
itemFound = true;
item = new RSSItem();
currentState = state_unknown;
} else if (localName.equalsIgnoreCase("title")) {
currentState = state_title;
} else if (localName.equalsIgnoreCase("description")) {
currentState = state_description;
} else if (localName.equalsIgnoreCase("link")) {
currentState = state_link;
} else if (localName.equalsIgnoreCase("pubdate")) {
currentState = state_pubdate;
} else {
currentState = state_unknown;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (itemFound == true) {
switch (currentState) {
case state_title:
item.setTitle(strCharacters.toString());
break;
case state_description:
break;
case state_link:
item.setLink(strCharacters.toString());
break;
case state_pubdate:
String dateStr = strCharacters.toString();
SimpleDateFormat curFormater = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
Date dateObj = null;
try {
dateObj = curFormater.parse(dateStr);
SimpleDateFormat postFormater = new SimpleDateFormat(
"dd.MM.yyyy HH:mm");
String newDateStr = postFormater.format(dateObj);
item.setPubdate(newDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
break;
default:
break;
}
} else {
switch (currentState) {
case state_title:
feed.setTitle(strCharacters.toString());
break;
case state_description:
break;
case state_link:
feed.setLink(strCharacters.toString());
break;
case state_pubdate:
feed.setPubdate(strCharacters.toString());
break;
default:
break;
}
}
currentState = state_unknown;
if (localName.equalsIgnoreCase("item")) {
feed.addItem(item);
}
}
public void startEntity(String name) throws SAXException {
inEntity = true;
entityName = name;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
strCharacters = new StringBuilder();
if (inEntity) {
inEntity = false;
strCharacters.append("&" + entityName + ";");
} else {
for (int i = start; i < start + length; i++) {
strCharacters.append(ch[i]);
}
}
// strCharacters.append(ch, start, length);
}
}
很難找到你做錯了什麼,但我可以建議你使用[XmlPullParser](http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html) –
好,謝謝。 XmlPullParser有什麼好的教程嗎? – user754730