• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

Help in sax2 xmlpull parser

                 <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
0
prgeorge3383
Asked:
prgeorge3383
  • 4
  • 3
  • 2
1 Solution
 
CEHJCommented:
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);
            }
      }
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
CEHJ, is that somebody's homework done :) ?

You can also use XPath, BTW.
0
 
CEHJCommented:
I hope not - i would guess that pull-parsing's not very 'homeworky'
0
Technology Partners: 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!

 
Mayank SAssociate Director - Product EngineeringCommented:
pull = null, I know ;-)
0
 
CEHJCommented:
:-)
0
 
prgeorge3383Author Commented:
I was just stuck , I was thinking start tag  event is genetated , only when it hits the OfficeDepot  label....
0
 
prgeorge3383Author Commented:
How do i make sure the officedepot label is the parent
0
 
CEHJCommented:
Please post a new question - only one allowed per thread (see site rules)
0
 
prgeorge3383Author Commented:
done
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now