2016-04-15 47 views
0

我正在爲Android構建一個RSS閱讀器,但我不知道我在做什麼錯誤。 我用這個教程:http://developer.android.com/training/basics/network-ops/xml.htmlXMLPullParser引發錯誤預期:開始標記

我總是得到一個錯誤:

/RSSXMLHELPER:: Error: org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}rss (position:START_DOCUMENT [email protected]:1 in [email protected])

這裏是我XMLPullParser類:

public class RSSXMLHelper { 

PostData postData; 
ArrayList<PostData> pList; 

public ArrayList<PostData> parse(InputStream in) throws XmlPullParserException, IOException { 
     if (in != null) { 
      try { 
       XmlPullParser parser = Xml.newPullParser(); 

       parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); 

       parser.setInput(in, null); 

       parser.nextTag(); 

       return readRss(parser); 
      } finally { 
       if (in != null) { 
        in.close(); 
       } 
      } 
     } else { 
      postData = new PostData(); 

      pList = new ArrayList<>(); 
      //l.add(entry); 
      return pList; 

     } 
} 

private ArrayList<PostData> readRss(XmlPullParser parser) 
     throws XmlPullParserException, IOException { 
    ArrayList<PostData> items = new ArrayList<>(); 
    parser.require(XmlPullParser.START_TAG, null, "rss"); 
    while (parser.next() != XmlPullParser.END_TAG) { 
     if (parser.getEventType() != XmlPullParser.START_TAG) { 
      continue; 
     } 
     String name = parser.getName(); 
     if (name.equals("channel")) { 
      items.addAll(readChannel(parser)); 
     } else { 
      skip(parser); 
     } 
    } 
    return items; 
} 

private ArrayList<PostData> readChannel(XmlPullParser parser) 
     throws IOException, XmlPullParserException { 
    ArrayList<PostData> items = new ArrayList<>(); 
    parser.require(XmlPullParser.START_TAG, null, "channel"); 
    while (parser.next() != XmlPullParser.END_TAG) { 
     if (parser.getEventType() != XmlPullParser.START_TAG) { 
      continue; 
     } 
     String name = parser.getName(); 
     if (name.equals("item")) { 
      items.add(readItem(parser)); 
     } else { 
      skip(parser); 
     } 
    } 
    return items; 
} 

private PostData readItem(XmlPullParser parser) throws XmlPullParserException, IOException { 
    PostData postData = new PostData(); 
    parser.require(XmlPullParser.START_TAG, null, "item"); 
    while (parser.next() != XmlPullParser.END_TAG) { 
     if (parser.getEventType() != XmlPullParser.START_TAG) { 
      continue; 
     } 
     String name = parser.getName(); 
     if (name.equals("title")) { 
      postData.setPostTitle(readTitle(parser)); 
     } else { 
      skip(parser); 
     } 
    } 
    return postData; 
} 

private String readTitle(XmlPullParser parser) 
     throws IOException, XmlPullParserException { 
    parser.require(XmlPullParser.START_TAG, null, "title"); 
    String title = readText(parser); 
    parser.require(XmlPullParser.END_TAG, null, "title"); 
    return title; 
} 

private String readText(XmlPullParser parser) 
     throws IOException, XmlPullParserException { 
    String result = ""; 
    if (parser.next() == XmlPullParser.TEXT) { 
     result = parser.getText(); 
     parser.nextTag(); 
    } 
    return result; 
} 

private void skip(XmlPullParser parser) throws XmlPullParserException, IOException { 
    if (parser.getEventType() != XmlPullParser.START_TAG) { 
     throw new IllegalStateException(); 
    } 
    int depth = 1; 
    while (depth != 0) { 
     switch (parser.next()) { 
      case XmlPullParser.END_TAG: 
       depth--; 
       break; 
      case XmlPullParser.START_TAG: 
       depth++; 
       break; 
     } 
    } 
} 

}

我尋找2小時的誤差,但我真的不知道我的代碼有什麼問題......

編輯:

XML文件:ifrick.ch/feed

+0

請張貼您的xml文件 –

+0

張貼鏈接到rss飼料 – Nick

回答

0

我知道了。代碼是正確的。

問題是我正在寫一個新的類,並把它放在現有類的後面進行測試。輸入流已經關閉,當涉及到新的XMLReader類...

相關問題