We help IT Professionals succeed at work.

Help in sax2 xmlpull parser

prgeorge3383
prgeorge3383 asked
on
Medium Priority
198 Views
Last Modified: 2013-11-23
                 <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.
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:
Try

package sax;

import java.io.IOException;
import java.io.StringReader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class SimpleXmlPullApp {

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

            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;
            while (count == null && eventType != XmlPullParser.END_DOCUMENT) {
                  if (eventType == XmlPullParser.START_TAG) {
                        if ("word32".equals(xpp.getName())) {
                              String label = xpp.getAttributeValue(null, "label");
                              if ("Count".equals(label)) {
                                    count = xpp.getAttributeValue(null, "value");
                              }
                        }
                  }
                  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
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
CEHJ, is that somebody's homework done :) ?

You can also use XPath, BTW.
CERTIFIED EXPERT
Top Expert 2016

Commented:
I hope not - i would guess that pull-parsing's not very 'homeworky'
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
pull = null, I know ;-)
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)

Author

Commented:
I was just stuck , I was thinking start tag  event is genetated , only when it hits the OfficeDepot  label....

Author

Commented:
How do i make sure the officedepot label is the parent
CERTIFIED EXPERT
Top Expert 2016

Commented:
Please post a new question - only one allowed per thread (see site rules)

Author

Commented:
done
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.