2014-06-08 2 views
1

자습서가 계속 표시되지만 도움이되지 않습니다. XmlPullParser를 사용하고 있지만 오류가 계속 발생하고 응용 프로그램에 xmlerror 페이지가 표시됩니다. 이는 형식 때문입니다. XML?안드로이드의 XML 파서에 문제가 있습니다.

public class XmlParser { 
    private static final String ns = null; 


    public Queue<Item> parse(InputStream in) throws XmlPullParserException, IOException { 

     try { 
      XmlPullParser parser = Xml.newPullParser(); 
      parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); 
      parser.setInput(in, null); 
      parser.nextTag(); 
      return readFeed(parser); 

     } finally { 
      in.close(); 
     } 
    } 

    private Queue<Item> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException { 
     Queue<Item> resp = new LinkedList<Item>(); 


     parser.require(XmlPullParser.START_TAG, ns, "channel"); 
     while (parser.next() != XmlPullParser.END_TAG) { 
      if (parser.getEventType() != XmlPullParser.START_TAG) { 
       continue; 
      } 
      String name = parser.getName(); 

      if (name.equals("item")) { 
       resp.add(readItem(parser)); 
      } else { 
       skip(parser); 
      } 
     } 
     return resp; 
    } 

    // This class represents a single entry (post) in the XML feed. 
    // It includes the data members "title," "link," and "summary." 
    public static class Item { 
     public final String title; 
     public final String link; 
     public final String summary; 

     private Item(String title, String summary, String link) { 
      this.title = title; 
      this.summary = summary; 
      this.link = link; 
     } 


    } 

    // Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them 
    // off 
    // to their respective &quot;read&quot; methods for processing. Otherwise, skips the tag. 
    private Item readItem(XmlPullParser parser) throws XmlPullParserException, IOException { 
     parser.require(XmlPullParser.START_TAG, ns, "item"); 
     String title = null; 
     String summary = null; 
     String link = null; 
     System.out.print("entrou readItem"); 
     while (parser.next() != XmlPullParser.END_TAG) { 
      if (parser.getEventType() != XmlPullParser.START_TAG) { 
       continue; 
      } 
      String name = parser.getName(); 
      if (name.equals("title")) { 
       title = readTitle(parser); 
      } else if (name.equals("description")) { 
       summary = readDescription(parser); 
      } else if (name.equals("link")) { 
       link = readLink(parser); 
      } else { 
       skip(parser); 
      } 
     } 
     return new Item(title, summary, link); 
    } 

    // Processes title tags in the feed. 
    private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException { 
     System.out.print("entrou title"); 
     parser.require(XmlPullParser.START_TAG, ns, "title"); 
     String title = readText(parser); 
     parser.require(XmlPullParser.END_TAG, ns, "title"); 
     return title; 
    } 

    // Processes link tags in the feed. 
    private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException { 
     parser.require(XmlPullParser.START_TAG, ns, "link"); 

     String link = readText(parser); 
     parser.require(XmlPullParser.END_TAG, ns, "link"); 
     return link; 
    } 

    // Processes summary tags in the feed. 
    private String readDescription(XmlPullParser parser) throws IOException, XmlPullParserException { 
     parser.require(XmlPullParser.START_TAG, ns, "description"); 
     String summary = readText(parser); 
     parser.require(XmlPullParser.END_TAG, ns, "description"); 
     return summary; 
    } 

    // For the tags title and summary, extracts their text values. 
    private String readText(XmlPullParser parser) throws IOException, XmlPullParserException { 
     String result = ""; 
     if (parser.next() == XmlPullParser.TEXT) { 
      result = parser.getText(); 
      parser.nextTag(); 
     } 
     return result; 
    } 

    // Skips tags the parser isn't interested in. Uses depth to handle nested tags. i.e., 
    // if the next tag after a START_TAG isn't a matching END_TAG, it keeps going until it 
    // finds the matching END_TAG (as indicated by the value of "depth" being 0). 
    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; 
      } 
     } 
    } 
} 

을 여기에 공급 내가로부터 정보를 끌어 시도하고있다 : 여기

내가 뭘하는지 당신이 사용하는 경우

http://feeds.folha.uol.com.br/poder/rss091.xml

+0

가능한 복제본 [XmlPullParser를 사용하여 RSS 피드를 구문 분석하는 방법] (http://stackoverflow.com/questions/17434135/how-to-parse-an-rss-feed-with-xmlpullparser) – matiash

답변

0

가 해결, 생각 XMLParser를 사용하면 1.0 이상에 액세스하고 있는지 확인해야합니다. XML 2.0을 사용할 때 매력처럼 작동합니다. 아무도 이것을 확인할 수 있습니까?

관련 문제