We help IT Professionals succeed at work.

xmlpull 3 in java parsing

prgeorge3383
prgeorge3383 asked
on
Medium Priority
206 Views
Last Modified: 2006-11-18
             <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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
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);
            }
      }
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2016

Commented:
Thanks, but why a B grade?

Author

Commented:
Sorry, I did it fast... let me change it... answer was cool......

Author

Commented:
how to change it
CERTIFIED EXPERT
Top Expert 2016

Commented:
Thanks. I'll get it reopened. Do nothing until notified
CERTIFIED EXPERT
Top Expert 2016

Commented:
Thanks both

Author

Commented:
cool thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.