0
<users type="array">
    <user>
       <id>aOlXzSQQKr3749eJe5cbCb</id>
       <login>name1</login>
       <login_name warning="deprecated">name1</login_name>
       <name>Name 1</name>
   </user>
  <user>
       <id>c8QDleB8Or36QseJe5cbLA</id>
       <login>name2</login>
       <login_name warning="deprecated">name2</login_name>
       <name>Nmae 2</name>
 </user>
</users >

Hi, i need to parse above XML using XMLPullParser for , and I need names in a array list. How can I do it, please suggest.

Thanks.

1 Answer 1

1

Maybe somthing like this should work:

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new FileReader(XMLFILEPATH));
int eventType = xpp.getEventType();
String lastTag;
while (eventType != XmlPullParser.END_DOCUMENT)
    {
        UserStruct user;

        /*
         * The name of the tag like: <foo> --> foo
         */
        String tagName = xpp.getName();
        /*
         * Opening tag
         */
        if (eventType == XmlPullParser.START_TAG)
        {
            if (tagName.equals("users"))
            {
                // init your ArrayList
                urArrayList.clear();
            } else if (tagName.equals("user"))
            {
                // new user tag opened
                user = new UserStruct();
            } else if(tagName.equals("id")) {
                lastTag = "id";
            }

            /*
             * Closing tag
             */
        } else if (eventType == XmlPullParser.END_TAG)
        {
            if (tagName.equals("user"))
            {
                urArrayList.add(user);
            }
        } else if (eventType == XmlPullParser.TEXT)
        {
            if (lastTag.equals("id"))
            {
                // fill UserSTruct
                // do so for the other tags as well
            }
        }

        eventType = xpp.next();
    }
Sign up to request clarification or add additional context in comments.

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.