0

我在解析 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>

有人可以帮我吗?

我想从喜欢标签评论标签标题标签和图片标签中获取数据。

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

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

4 回答 4

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

于 2014-05-14T08:58:24.557 回答
0

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

于 2014-05-14T08:05:49.780 回答
0

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

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

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

于 2014-05-14T08:05:50.047 回答
0

例如, 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 个 Map,前两个 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}
于 2015-06-12T04:29:23.747 回答