2

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

使用 XmlPullParser 并在第 80 行,该值始终等于“通道”。当它调用 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) 

这是解析器类:

    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>

提前谢谢大家:)

4

1 回答 1

3

带有我自己的示例数据的 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");
于 2014-07-17T04:02:57.163 回答