0

I am having difficulties in parsing XML in android. i have the following 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>

Can anybody help me with this?

I want to get data from Likes tag comments tag title tag and picture tag.

Heres what i have been trying to do.

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;
    }
10
  • did you check my post? working? Commented May 15, 2014 at 17:32
  • Yes i checked it.. it was very helpful, +1 but it could'nt help me solve my problem completely. Commented May 17, 2014 at 10:26
  • does it work for you is the question? Commented May 17, 2014 at 10:26
  • actually, i have stopped working on the parsing thing right now, will get back on parsing in a couple of days then i will apply your method completely, than we will see if it is working completely or not. Commented May 17, 2014 at 10:27
  • no hurry take your time Commented May 17, 2014 at 10:28

4 Answers 4

1

To parse

 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;

    }

To skip tags

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

The Log

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

More information read

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

instead of providing you with a temp way to achieve your case, just have a look at the DOM parser and XPath and they will give you a general way to parse XML in Android or Java at all

Comments

0

Why not try to parse with JAXB parser?

JAXB is a java parser for XMLs and makes your life really simple when you need to parse XMLs.

Some idea on how to use JAXB is in my blog here

Comments

0

https://github.com/Cruisoring/EasyXML provides a means to parse XML to Maps, for example.

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

The result is 12 Map for the 12 books, for the first two XML elements shown below:

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

The corresponding first two Map are printed out:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.