Parse XML android



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

解析

 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

不提供临时的方法来实现你的情况,看一下DOM解析器和XPath它们会给你一个在Android或Java中解析XML的通用方法

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

JAXB是xml的java解析器,当您需要解析xml时,它使您的工作变得非常简单。

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

https://github.com/Cruisoring/EasyXML提供了一种将XML解析为Maps的方法。

@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 Map,用于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}

相关内容

  • 没有找到相关文章

最新更新