首先,我是这个java / android开发世界的新手,所以对我来说很遗憾,我可能会问一些相对新手的问题:)。 无论如何,我现在一整天都在解决这个问题,而且我无法通过自己找出任何解决方案,而且我在网上搜索能够绕过这个问题的想法。
我正在尝试开发一个解析外部XML文件数据的Android应用程序。
我的解析器看起来像这样:
public class NewSAXHandler implements ContentHandler
{
private String DEBUGTAG = "NewSAXHandler";
public static setNews news = null;
boolean currentElement = false;
String currentValue = null;
public static setNews getNews()
{
return news;
}
public static void setNewsList(setNews news)
{
NewSAXHandler.news = news;
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void startElement(String uri, String localName, String qname, Attributes attr) throws SAXException
{
currentElement = true;
if (localName.equalsIgnoreCase("channel"))
news = new setNews();
Log.d(DEBUGTAG, localName);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
if (localName.equalsIgnoreCase("title"))
{
news.setHeadline(currentValue);
Log.d(DEBUGTAG, localName);
Log.d(DEBUGTAG, currentValue);
}
else if (localName.equalsIgnoreCase("pubdate"))
{
news.setDate(currentValue);
Log.d(DEBUGTAG, localName);
Log.d(DEBUGTAG, currentValue);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
if (currentElement)
{
currentValue = new String(ch, start, length).replaceAll("\\r\\n|\\r|\\n", " ");
currentElement = false;
}
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length)throws SAXException
{
}
@Override
public void endPrefixMapping(String prefix) throws SAXException
{
}
@Override
public void processingInstruction(String target, String data)throws SAXException
{
}
@Override
public void setDocumentLocator(Locator locator)
{
}
@Override
public void skippedEntity(String name) throws SAXException
{
}
@Override
public void startPrefixMapping(String prefix, String uri)throws SAXException
{
}
}
XML文件解析自:
http://www.hltv.org/news.rss.php
以下是我运行应用时的日志:
10-24 20:03:32.901: D/NewSAXHandler(975): rss
10-24 20:03:32.901: D/NewSAXHandler(975): channel
10-24 20:03:32.901: D/NewSAXHandler(975): title
10-24 20:03:32.901: D/NewSAXHandler(975): title
10-24 20:03:32.901: D/NewSAXHandler(975): www.HLTV.org News
10-24 20:03:32.901: D/NewSAXHandler(975): link
10-24 20:03:32.912: D/NewSAXHandler(975): description
10-24 20:03:32.912: D/NewSAXHandler(975): item
10-24 20:03:32.912: D/NewSAXHandler(975): title
10-24 20:03:32.912: D/NewSAXHandler(975): title
10-24 20:03:32.912: D/NewSAXHandler(975): http://www.hltv.org/HLTV.org News
10-24 20:03:32.912: D/NewSAXHandler(975): Photos: Final ones from ESWC
10-24 20:03:32.912: D/NewSAXHandler(975): link
10-24 20:03:32.912: D/NewSAXHandler(975): pubDate
10-24 20:03:32.922: D/NewSAXHandler(975): pubDate
10-24 20:03:32.922: D/NewSAXHandler(975): http://www.hltv.org/news/7692-photos-final-ones-from-eswcMon, 24 Oct 2011 21:17:00 +0200
10-24 20:03:32.922: D/NewSAXHandler(975): item
10-24 20:03:32.922: D/NewSAXHandler(975): title
10-24 20:03:32.932: W/System.err(975): org.apache.harmony.xml.ExpatParser$ParseException: At line 16, column 23: not well-formed (invalid token)
10-24 20:03:32.942: W/System.err(975): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:520)
10-24 20:03:32.952: W/System.err(975): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
10-24 20:03:32.952: W/System.err(975): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
10-24 20:03:32.952: W/System.err(975): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
10-24 20:03:32.962: W/System.err(975): at jj.rssReader.hltvorg.Hltvorg.onCreate(Hltvorg.java:49)
10-24 20:03:32.962: W/System.err(975): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-24 20:03:32.962: W/System.err(975): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-24 20:03:32.971: W/System.err(975): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-24 20:03:32.971: W/System.err(975): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-24 20:03:32.981: W/System.err(975): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-24 20:03:32.981: W/System.err(975): at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 20:03:32.981: W/System.err(975): at android.os.Looper.loop(Looper.java:123)
10-24 20:03:32.992: W/System.err(975): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-24 20:03:32.992: W/System.err(975): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 20:03:33.002: W/System.err(975): at java.lang.reflect.Method.invoke(Method.java:507)
10-24 20:03:33.002: W/System.err(975): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-24 20:03:33.002: W/System.err(975): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-24 20:03:33.013: W/System.err(975): at dalvik.system.NativeStart.main(Native Method)
似乎错误来自'角色 我看不到编码,因为它不在XML文件中,但我猜它是UTF-8 我也尝试使用StringBuilder来存储每个角色而没有任何运气。
我认为XML解析器会自行转换这些特殊字符,但似乎它不喜欢它。
如果我尝试解析此文件:
http://www.hltv.org/forum.rss.php
然后它会更好。
有人有任何新想法吗?
**如果您需要我的代码,请说出来:)
最诚挚的问候,
的Jesper
答案 0 :(得分:2)
问题是Philipp上面说的编码。
我刚刚在我的代码中添加了以下内容:
InputSource is = new InputSource(url.openStream());
is.setEncoding("ISO-8859-1");
Reader.parse(is);