【问题标题】:Parse XML android解析 XML 安卓
【发布时间】:2014-05-14 08:00:09
【问题描述】:

我在 android 中解析 XML 时遇到了困难。我有以下 XML

<iq xmlns="jabber:client" type="result" to="blob@faisal-system/68bb97e7">
  <album xmlns="naseebalbum">
    <albumpicture>
      <title>day1</title>
      <creationdate>1397502000000</creationdate>
      <picture>BASE64EncodedStringOfImage</picture>
    </albumpicture>
    <comments>
      <comment>
        <commentid>1</commentid>
        <username>sana</username>
        <text>i loved that pic</text>
        <commenttime>1398264140000</commenttime>
      </comment>
    </comments>
    <likes>
      <like>
        <likeid>4</likeid>
        <username>sana</username>
        <liketime>1398250919000</liketime>
      </like>
    </likes>
  </album>
</iq>

谁能帮我解决这个问题?

我想从 Likes 标签 cmets 标签 title 标签和图片标签中获取数据。

这就是我一直在尝试做的事情。

public IQ parseIQ(XmlPullParser parser) throws Exception {
        // TODO Auto-generated method stub
        payload=""+parser.getText();

         StringBuilder sb = new StringBuilder();
            int depth = 1;
            while (depth != 0) {

                switch (parser.next()) {
                case XmlPullParser.END_TAG:
                    depth--;
                    if (depth > 0) {
                        sb.append("</" + parser.getName() + ">");
                    }
                    break;
                case XmlPullParser.START_TAG:
                    depth++;
                    StringBuilder attrs = new StringBuilder();
                    for (int i = 0; i < parser.getAttributeCount(); i++) {
                        attrs.append(parser.getAttributeName(i) + "=\""
                                + parser.getAttributeValue(i) + "\" ");
                    }
                    sb.append("<" + parser.getName() + " " + attrs.toString() + ">");

                    break;
                default:
                    sb.append(parser.getText());
                    break;
                }
            }
            payload = sb.toString();

        iq=new CustomIQ(payload);
        iq.setType(Type.RESULT);

        return iq;
    }

【问题讨论】:

  • 你查看我的帖子了吗?工作吗?
  • 是的,我检查了它。它非常有帮助,+1 但它不能帮助我完全解决我的问题。
  • 问题对你有用吗?
  • 实际上,我现在已经停止了解析工作,几天后会重新解析,然后我会完全应用你的方法,然后我们会看看它是否完全工作.
  • 不着急慢慢来

标签: android xml android-xmlpullparser


【解决方案1】:

解析

 public Void parse(InputStream is) {
       try
       {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        xpp.setInput(is, null);

        xpp.nextTag();
        xpp.require(XmlPullParser.START_TAG, null, "iq");
        while (xpp.nextTag() == XmlPullParser.START_TAG) {

            xpp.require(XmlPullParser.START_TAG, null, "album");
            xpp.nextTag();

            xpp.require(XmlPullParser.START_TAG, null, "albumpicture");
            xpp.nextTag();

            xpp.require(XmlPullParser.START_TAG, null, "title");
            Log.i("title is....",""+xpp.nextText());
            xpp.require(XmlPullParser.END_TAG, null, "title");
            xpp.nextTag();


            xpp.require(XmlPullParser.START_TAG, null, "creationdate");
            Log.i("creation date i....",""+xpp.nextText());
            xpp.require(XmlPullParser.END_TAG, null, "creationdate");
            xpp.nextTag();

            xpp.require(XmlPullParser.START_TAG, null, "picture");
            Log.i("picture is....",""+xpp.nextText());
            xpp.require(XmlPullParser.END_TAG, null, "picture");

            xpp.nextTag();
            xpp.require(XmlPullParser.END_TAG, null, "albumpicture");
            xpp.nextTag();

            xpp.require(XmlPullParser.START_TAG, null, "comments");
            xpp.nextTag();


            xpp.require(XmlPullParser.START_TAG, null, "comment");

            xpp.nextTag();
            xpp.require(XmlPullParser.START_TAG, null, "commentid");
            Log.i("comment id is....",""+xpp.nextText());
            xpp.require(XmlPullParser.END_TAG, null, "commentid");

            xpp.nextTag();
            xpp.require(XmlPullParser.START_TAG, null, "username");
            Log.i("username is....",""+xpp.nextText());
            xpp.require(XmlPullParser.END_TAG, null, "username");

            xpp.nextTag();
            xpp.require(XmlPullParser.START_TAG, null, "text");
            Log.i("text is....",""+xpp.nextText());
            xpp.require(XmlPullParser.END_TAG, null, "text");

            xpp.nextTag();
            xpp.require(XmlPullParser.START_TAG, null, "commenttime");
            Log.i("comment is....",""+xpp.nextText());
            xpp.require(XmlPullParser.END_TAG, null, "commenttime");

            xpp.nextTag();
            xpp.require(XmlPullParser.END_TAG, null, "comment");

            xpp.nextTag();
            xpp.require(XmlPullParser.END_TAG, null, "comments");
            xpp.nextTag();

            xpp.require(XmlPullParser.START_TAG, null, "likes");
            xpp.nextTag();


            xpp.require(XmlPullParser.START_TAG, null, "like");

            xpp.nextTag();
            xpp.require(XmlPullParser.START_TAG, null, "likeid");
            Log.i("like id is....",""+xpp.nextText());
            xpp.require(XmlPullParser.END_TAG, null, "likeid");

            xpp.nextTag();
            xpp.require(XmlPullParser.START_TAG, null, "username");
            Log.i("username is....",""+xpp.nextText());
            xpp.require(XmlPullParser.END_TAG, null, "username");

            xpp.nextTag();
            xpp.require(XmlPullParser.START_TAG, null, "liketime");
            Log.i("liketime is....",""+xpp.nextText());
            xpp.require(XmlPullParser.END_TAG, null, "liketime");

            xpp.nextTag();
            xpp.require(XmlPullParser.END_TAG, null, "like");

            xpp.nextTag();
            xpp.require(XmlPullParser.END_TAG, null, "likes");

            xpp.nextTag();
            xpp.require(XmlPullParser.END_TAG, null, "album");

        }
        xpp.require(XmlPullParser.END_TAG, null, "iq");
       }catch(Exception e)
       {
           e.printStackTrace();
       }
        return null;

    }

跳过标签

     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;
                }
            }
         }

日志

05-14 07:03:25.214: I/title is....(2212): day1
05-14 07:03:25.214: I/creation date i....(2212): 1397502000000
05-14 07:03:25.214: I/picture is....(2212): BASE64EncodedStringOfImage
05-14 07:03:25.214: I/comment id is....(2212): 1
05-14 07:03:25.224: I/username is....(2212): sana
05-14 07:03:25.224: I/text is....(2212): i loved that pic
05-14 07:03:25.224: I/comment is....(2212): 1398264140000
05-14 07:03:25.224: I/like id is....(2212): 4
05-14 07:03:25.224: I/username is....(2212): sana
05-14 07:03:25.224: I/liketime is....(2212): 1398250919000

阅读更多信息

http://developer.android.com/training/basics/network-ops/xml.html

http://androidcookbook.com/Recipe.seam?recipeId=2217

【讨论】:

    【解决方案2】:

    不是为您提供一种临时方法来实现您的案例,而是查看DOM parserXPath,它们将为您提供一种在 Android 或 Java 中解析 XML 的通用方法

    【讨论】:

      【解决方案3】:

      为什么不尝试使用 JAXB 解析器进行解析?

      JAXB 是一个用于 XML 的 java 解析器,当您需要解析 XML 时,它让您的生活变得非常简单。

      关于如何使用 JAXB 的一些想法在我的博客 here

      【讨论】:

        【解决方案4】:

        https://github.com/Cruisoring/EasyXML 提供了一种将 XML 解析为地图的方法,例如。

        @Test
        public void testDocument_mapOf() {
        
            URL url = Thread.currentThread().getContextClassLoader()
                .getResource("books.xml");
        
            Document doc = EasySAXParser.parse(url);
        
            List<? extends Map<String, String>> maps = doc.mapOf("book");
        
            System.out.println(maps.get(0));
        
            System.out.println(maps.get(1));
        }
        

        结果是 12 本书的 12 个地图,前两个 XML 元素如下所示:

           <book id="bk101">
              <author>Gambardella, Matthew</author>
              <title>XML Developer's Guide</title>
              <genre>Computer</genre>
        <!--       <price>44.95</price> -->
              <publish_date>2000-10-01</publish_date>
              <description>An in-depth look at creating applications 
              with XML.</description>
           </book>
           <book id="bk102">
              <author>Ralls, Kim</author>
              <title>Midnight Rain</title>
              <genre>Fantasy</genre>
              <price>5.95</price>
              <publish_date>2000-12-16</publish_date>
              <description>A former architect battles corporate zombies, 
              an evil sorceress, and her own childhood to become queen 
              of the world.</description>
           </book>
        

        打印出对应的前两个Map:

        {author=Gambardella, Matthew, genre=Computer, description=An in-depth look at creating applications 
              with XML., id=bk101, title=XML Developer's Guide, publish_date=2000-10-01}
        {author=Ralls, Kim, price=5.95, genre=Fantasy, description=A former architect battles corporate zombies, 
              an evil sorceress, and her own childhood to become queen of the world., id=bk102, title=Midnight Rain, publish_date=2000-12-16}
        

        【讨论】:

          猜你喜欢
          • 2011-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-28
          • 2015-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多