xmlpull 3 in java parsing

             <OfficeDepot label="OD" version="7">
                    <word32 label="Zip" value="7220"/>
                    <word32 label="Street" value="1934"/>
                    <word32 label="Type" value="341"/>
                    <word32 label="Count" value="400"/>
                    <word32 label="Open" value="145"/>
               </<OfficeDepot >


How can I extract the value of count in this xml string which is under the parent  officedepot .
In the start tag event I am able to idetify Officedepot as a tag. How do I traverse down and get the  value for count
Thanks for all the help
prgeorge3383Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
This is really moving away from SAX towards DOM but you can do it by saving OfficeDepot on a 'stack':


public class SimpleXmlPullApp {

      public static void main(String args[]) throws XmlPullParserException,
      
                  IOException {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            boolean officeDepotIsCurrent = false;

            String input = "<OfficeDepot label=\"OD\" version=\"7\">"
                        + "<word32 label=\"Zip\" value=\"7220\"/>"
                        + "<word32 label=\"Street\" value=\"1934\"/>"
                        + "<word32 label=\"Type\" value=\"341\"/>"
                        + "<word32 label=\"Count\" value=\"400\"/>"
                        + "<word32 label=\"Open\" value=\"145\"/>" + "</OfficeDepot>";
            xpp.setInput(new StringReader(input));
            int eventType = xpp.getEventType();
            String count = null;
            String tagName = null;
            while (count == null && eventType != XmlPullParser.END_DOCUMENT) {
                  if (eventType == XmlPullParser.START_TAG) {
                        tagName = xpp.getName();
                        if ("OfficeDepot".equals(tagName)) {
                              officeDepotIsCurrent = true;
                        }
                        else if (officeDepotIsCurrent && "word32".equals(tagName)) {
                              String label = xpp.getAttributeValue(null, "label");
                              if ("Count".equals(label)) {
                                    count = xpp.getAttributeValue(null, "value");
                              }
                        }
                  }
                  else if (officeDepotIsCurrent && eventType == XmlPullParser.END_TAG) {
                        if (tagName.equals("OfficeDepot")) {
                              officeDepotIsCurrent = false;
                        }
                  }
                  eventType = xpp.next();
            }
            if (count != null) {
                  System.out.printf("count=%s\n", count);
            }
      }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Thanks, but why a B grade?
0
prgeorge3383Author Commented:
Sorry, I did it fast... let me change it... answer was cool......
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

prgeorge3383Author Commented:
how to change it
0
CEHJCommented:
Thanks. I'll get it reopened. Do nothing until notified
0
CEHJCommented:
Thanks both
0
prgeorge3383Author Commented:
cool thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.