我是XML Parsing的新手,我需要获取url feed并解析它们,然后将它们保存在本地数据库中。如果我传递一些其他链接它接受,但如果我通过我需要的链接它给出这个错误。 我还提供了我的代码部分和log cat输出。 谁能告诉我我面临的问题是什么?
以下是我需要解析的网址。
private static final String POSTS_URL = "http://bgr.in/feed/iphoneapp";
这是产生错误的方法: -
public static void updateStories(Context context, String url, int type) {
if (StoryService.isConnected(context)) {
HttpGet request = new HttpGet(url);
request.addHeader("accepts", "application/rss+xml");
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
try {
response = client.execute(request);
} catch (IOException e) {
Log.e(TAG, "Failed to get rss feed.", e);
}
RootElement root = new RootElement("rss");
List<BaseStory> stories = Story.appendArrayListener(type,
root.getChild("channel"), 0);
try {
Xml.parse(response.getEntity().getContent(), Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "Failed to parse " + url, e);
}`public static void updateStories(Context context, String url, int type) {
if (StoryService.isConnected(context)) {
HttpGet request = new HttpGet(url);
request.addHeader("accepts", "application/rss+xml");
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
try {
response = client.execute(request);
} catch (IOException e) {
Log.e(TAG, "Failed to get rss feed.", e);
}
RootElement root = new RootElement("rss");
List<BaseStory> stories = Story.appendArrayListener(type,
root.getChild("channel"), 0);
try {
Xml.parse(response.getEntity().getContent(), Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "Failed to parse " + url, e);
}
Log Cat输出如下: -
System.err(19251): org.apache.harmony.xml.ExpatParser$ParseException: At line 292,column 0: junk after document element
System.err(19251): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:515)
System.err(19251): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
System.err(19251): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:316)
System.err(19251): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
System.err(19251): at android.util.Xml.parse(Xml.java:84)
System.err(19251): at in.bgr.service.StoryService.updateStories(StoryService.java:122)
System.err(19251): at in.bgr.service.StoryService.updateStories(StoryService.java:96)
System.err(19251): at in.bgr.ui.AllStoriesFragment$LoadStoriesTask.doInBackground(AllStoriesFragment.java:309)
System.err(19251): at in.bgr.ui.AllStoriesFragment$LoadStoriesTask.doInBackground(AllStoriesFragment.java:1)
System.err(19251): at android.os.AsyncTask$2.call(AsyncTask.java:288)
System.err(19251): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
System.err(19251): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
System.err(19251): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
答案 0 :(得分:1)
“文档元素之后的垃圾”错误是在根元素标记之后的xml中有任何不需要的标记时引起的。只删除根元素标记后面的所有标记。 在我的例子中,XML元素在XML文件中的行号292处重复输入。我清除了它,现在我觉得它对我有用。