XmlPullParser没有返回结果(org.xmlpull.v1.XmlPullParserException)



因为我的搭档是偏狂,所以不得不重写一些方法,哈哈。如果你看到任何拼写错误,可能是因为这个原因,但如果你认为它们可能导致问题,请指出它们

使用XmlPullParser,在第80行,值总是等于'Channel'。当调用skip时,parser.next()总是等于4。这段代码以前是可以工作的(由于学校的原因把它放在一边),现在它什么也不返回。它似乎跳过了所有的内部标签。

知道为什么吗?

更新:看起来我得到一个org.xmlpull.v1.XmlPullParserException。当我将异常打印到日志中时,我得到了以下结果。我认为它说它在寻找一个开始标签但它返回null(?):

07-17 00:02:14.564: E/XmlPullParserException(13604): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}channel (position:START_TAG <rss xmlns:a10='http://www.w3.org/2005/Atom' version='2.0'>@2:60 in java.io.InputStreamReader@4259cca8) 

下面是Parser类:

    import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import android.util.Xml;
public class Parser {
    // XML node keys
    private final String KEY_ITEM = "item"; // parent node
    private final String KEY_GUID = "guid";
    private final String KEY_LINK = "link";
    private final String KEY_TITLE = "title";
    private final String KEY_DESCRIPTION = "description";
    private final String KEY_UPDATED = "updated";
    public static final String nameSpace = null;

    public void parse(InputStream in, String type)
            throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            readFeed(parser, type);
        } finally {
            in.close();
        }
    }
    private void readFeed(XmlPullParser parser, String type)
            throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, nameSpace, null);
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName(); // This is line 80
            Log.e("READFEED", String.valueOf(name));
            if (name.equals(KEY_ITEM)) {
                DatabaseManager.sInstance.addItem(readItem(parser, type));
            } else {
                skip(parser);
            }
        }
    }
    private Item readItem(XmlPullParser parser, String sportType)
            throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, nameSpace, KEY_ITEM);
        Log.e("readItem", "readItem");
        long id = 0;
        String link = null;
        int linkType = 0;
        String title = null;
        String description = null;
        String updated = null;
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            Log.e("readItem", name);
            if (name.equalsIgnoreCase(KEY_LINK)) {
                link = readTag(parser, KEY_LINK);
                if (link.contains("player")) {
                    linkType = 1;
                }
            } else if (name.equalsIgnoreCase(KEY_TITLE)) {
                title = readTag(parser, KEY_TITLE);
            } else if (name.equalsIgnoreCase(KEY_DESCRIPTION)) {
                description = readTag(parser, KEY_DESCRIPTION);
            } else if (name.equalsIgnoreCase(KEY_GUID)) {
                id = Long.valueOf(readTag(parser, KEY_GUID).replace(" ", ""));
            } else if (name.equalsIgnoreCase(KEY_UPDATED)) {
                updated = readTag(parser, KEY_UPDATED);
                Pattern regex = Pattern.compile("T*");
                Matcher regexMatcher = regex.matcher(updated);
                if (regexMatcher.find()) {
                    String date = regexMatcher.group();
                }
            } else {
                skip(parser);
            }
        }
        return DatabaseManager.sInstance.newItem(id, sportType, link, linkType, title,
                description, updated);
    }
    // Processes description tags in the feed.
    private String readTag(XmlPullParser parser, String tag)
            throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, nameSpace, tag);
        String text = readText(parser);
        parser.require(XmlPullParser.END_TAG, nameSpace, tag);
        return text;
    }
    // For the tags title and description, 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;
    }
    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;
            }
        }
    }
}

这是代码从

中提取的XML结构
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <title></title>
        <link></link>
        <description></description>
        <language></language>
        <lastBuildDate></lastBuildDate>
        <item>
            <guid isPermaLink="false"></guid>
            <link></link>
            <title></title>
            <description></description>
            <a10:updated></a10:updated>
        </item>
        <item>
            <guid isPermaLink="false"></guid>
            <link></link>
            <title></title>
            <description></description>
            <a10:updated></a10:updated>
        </item>
        <item>
            <guid isPermaLink="false"></guid>
            <link></link>
            <title></title>
            <description></description>
            <a10:updated></a10:updated>
        </item>
        <item>
            <guid isPermaLink="false"></guid>
            <link></link>
            <title></title>
            <description></description>
            <a10:updated></a10:updated>
        </item>
    </channel>
</rss>

提前感谢大家:)

带有我自己的示例数据的xml

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <title>Title</title>
        <link>Title Link</link>
        <description>Description</description>
        <language>Language</language>
        <lastBuildDate>Build</lastBuildDate>
        <item>
            <guid isPermaLink="false">Id1</guid>
            <link>Link1</link>
            <title>Title1</title>
            <description>description1</description>
            <a10:updated>updated1</a10:updated>
        </item>
        <item>
            <guid isPermaLink="false">Id2</guid>
            <link>Link2</link>
            <title>Title2</title>
            <description>Description2</description>
            <a10:updated>updated2</a10:updated>
        </item>
        <item>
            <guid isPermaLink="false">Id3</guid>
            <link>Link3</link>
            <title>Title3</title>
            <description>Description3</description>
            <a10:updated>updated3</a10:updated>
        </item>
        <item>
            <guid isPermaLink="false">Id4</guid>
            <link>Link4</link>
            <title>Title4</title>
            <description>Descriptiob4</description>
            <a10:updated>updated4</a10:updated>
        </item>
    </channel>
</rss> 

下面是解析样例

public void parse(InputStream is)
  {
      try
      {
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(false);
      XmlPullParser xpp = factory.newPullParser();
      xpp.setInput(is,null);
      boolean insideItem = false;
      // Returns the type of current event: START_TAG, END_TAG, etc..
      int eventType = xpp.getEventType();
      while (eventType != XmlPullParser.END_DOCUMENT) {
          if (eventType == XmlPullParser.START_TAG) {

              if (xpp.getName().equalsIgnoreCase("item")) {
                  insideItem = true;
              } else if (xpp.getName().equalsIgnoreCase("guid")) {
                  if (insideItem)
                      Log.i("Guid is",xpp.nextText()); 
              } else if (xpp.getName().equalsIgnoreCase("link")) {
                  if (insideItem)
                      Log.i("Link is",xpp.nextText());  
              }
              else if (xpp.getName().equalsIgnoreCase("title")) {
                  if (insideItem)
                      Log.i("Title is.",xpp.nextText());  
              }    
              else if (xpp.getName().equalsIgnoreCase("description")) {
                  if (insideItem)
                      Log.i("Description is.",xpp.nextText());  
              }
              else if (xpp.getName().equalsIgnoreCase("a10:updated")) {
                  if (insideItem)
                      Log.i("Updated url is.",xpp.nextText());  
              }

          } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
              insideItem = false;
          }
          eventType = xpp.next(); /// move to next element
      }
      }catch(Exception e)
      {
          e.printStackTrace();
      }
  }
日志

07-17 09:30:09.165: I/Guid is(11781): Id1
07-17 09:30:09.165: I/Link is(11781): Link1
07-17 09:30:09.165: I/Title is.(11781): Title1
07-17 09:30:09.165: I/Description is.(11781): description1
07-17 09:30:09.165: I/Updated url is.(11781): updated1
07-17 09:30:09.165: I/Guid is(11781): Id2
07-17 09:30:09.165: I/Link is(11781): Link2
07-17 09:30:09.165: I/Title is.(11781): Title2
07-17 09:30:09.165: I/Description is.(11781): Description2
07-17 09:30:09.165: I/Updated url is.(11781): updated2
07-17 09:30:09.165: I/Guid is(11781): Id3
07-17 09:30:09.165: I/Link is(11781): Link3
07-17 09:30:09.165: I/Title is.(11781): Title3
07-17 09:30:09.165: I/Description is.(11781): Description3
07-17 09:30:09.170: I/Updated url is.(11781): updated3
07-17 09:30:09.170: I/Guid is(11781): Id4
07-17 09:30:09.170: I/Link is(11781): Link4
07-17 09:30:09.170: I/Title is.(11781): Title4
07-17 09:30:09.170: I/Description is.(11781): Descriptiob4
07-17 09:30:09.170: I/Updated url is.(11781): updated4

注意:只解析了带有item标签的所有数据

编辑:

你有

parser.require(XmlPullParser.START_TAG, nameSpace, null);

应该是

parser.require(XmlPullParser.START_TAG, nameSpace, "rss");

相关内容

  • 没有找到相关文章

最新更新