简体   繁体   中英

Xmlpullparser parsing nested items

I am making an app that will access the ebay api via xml to search for items. I have followed the guide Android developer guide XMLPullParser to use Pull parser, and I am able to parse basic xml fine. BUT for my needs there is not enough info on pullparser, as I am trying to access items in a format not mentioned in neither the link nor the pullparser site.

Product Items are in the Item Array. When trying to run the parser after getting the 1st item returned, the parser moves back to the Item array level instead of looping for each item. This results in the parser running through the whole xml and only returning one item. After trying for a few days, nothing I tried keeps the parser from returning to the Item Array level.

This is the structure of the XML

     <FindPopularItemsResponse>
     <Timestamp>2013-04-08T18:30:44.139Z</Timestamp>
    <Ack>Success</Ack>
    <Build>E817_CORE_APILW2_15902151_R1</Build>
    <Version>817</Version>
    <ItemArray>
     <ItemID>330624952975</ItemID><EndTime>2013-05-03T13:31:06.000Z</EndTime> 
      <ViewItemURLForNaturalSearch></ViewItemURLForNaturalSearch>
       <ListingType>FixedPriceItem</ListingType><GalleryURL></GalleryURL>
        <PrimaryCategoryID>73522</PrimaryCategoryID><PrimaryCategoryName>  
        </PrimaryCategoryName><BidCount>706</BidCount><ConvertedCurrentPrice 
         currencyID="USD">14.95</ConvertedCurrentPrice>
        <ListingStatus>Active</ListingStatus><TimeLeft>P24DT19H22S</TimeLeft>
        <Title>    </Title>
    </ItemArray>
    </FindPopularItemsResponse>

Thanks

Luke

Without any code this one is actually a tough one to answer.

Your loop structure should probably look like that:

while (eventType != XmlPullParser.END_DOCUMENT) {
    switch (eventType) {
    case XmlPullParser.START_TAG:
        if (xpp.getName().compareTo("ItemID") == 0) { // new item found
            // handle the whole item here if the format is static
        }
        // other stuff
        break;
    case XmlPullParser.END_TAG:
        // TODO
        break;
    case XmlPullParser.TEXT:
        // TODO
        break;
    }

There's only one item in the xml you provide. Without multiple items in your example its really difficult to answer correctly. But I'll try for just ItemID. Create a List of ItemID and when you hit text for that node add a new ItemID to the list..

create a list of your object and keep track of the current node

List<ItemID> bdxr = new ArrayList<ItemID>();

...

String N = "";  // I know it's upper case but its one character and important

...

if (eventType == XmlPullParser.START_TAG) {
    N = xpp.getName(); // current node

...


} else if (eventType == XmlPullParser.TEXT) {
if (N.equals("ItemID")) {
        // create an ItemID Object and add the text to it
        bdxr.add(new ItemID(xpp.getText()); 

...


} else if (eventType == XmlPullParser.END_TAG) {
    N = ""; // no current node.

Good Luck with adding items to a list a simple xpp loop

You haven't quite specified what data you are trying to pull from the xml, so....assuming you want data from each of the tags, here is how i'd do it

int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
        String name;

        String itemID, endTime, viewItemURLForNaturalSearch, listingType;

        switch (eventType) {
            case XmlPullParser.START_DOCUMENT:
                Log.d("Ebay  ", " Start of Document");
                break;

            //look for the starting tag "item array" in the xml
            case XmlPullParser.START_TAG:


                name = parser.getName();
                if(name.equalsIgnoreCase("ItemArray")) {

           /*to handle nested tags. 
           "parser.nextTag()" goes to the next starting tag  immediately following "ItemArray",
           and "itemID = parser.nextText()" assigns the text within that tag 
            to the string itemID*/

                    parser.nextTag();
                    itemID = parser.nextText();
                    Log.d("Listing ", itemID);

                    parser.nextTag();
                    endTime = parser.nextText();
                    Log.d("Listing ", endTime);

                    parser.nextTag();
                    viewItemURLForNaturalSearch = parser.nextText();
                    Log.d("Listing ", viewItemURLForNaturalSearch);

                    parser.nextTag();
                    listingType = parser.nextText();
                    Log.d("Listing ", listingType);

                }


                break;

        }

        eventType = parser.next();
    }

hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM