Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

xmlpull 3 in java parsing

Posted on 2006-04-02
8
Medium Priority
?
188 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
0
Comment
Question by:prgeorge3383
  • 4
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 16356156
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16362498
Thanks, but why a B grade?
0
 

Author Comment

by:prgeorge3383
ID: 16363348
Sorry, I did it fast... let me change it... answer was cool......
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:prgeorge3383
ID: 16363358
how to change it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16364192
Thanks. I'll get it reopened. Do nothing until notified
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16364796
Thanks both
0
 

Author Comment

by:prgeorge3383
ID: 16365343
cool thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question