Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8988371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:00:06+00:00 2026-06-15T22:00:06+00:00

I have a XML where it will represent a product catalog, in which each

  • 0

I have a XML where it will represent a product catalog, in which each product belongs to a category. ie:

<Product category="ABC">
 <Item_number>123</Item_number>
</Product>

<Product category="ABC">
 <Item_number>456</Item_number>
</Product>

<Product category="XYZ">
 <Item_number>789</Item_number>
</Product>

I have created a class to store the data, and then put an instance into an array.* Then displaying it onto a tableview .. Currently, tableview shows a list of Item_numbers 😀

What i’m trying to achieve is the same, but I wish to have 2 pages. The main tableview with non repeating (unique) category (ie. ABC, XYZ), and on the 2nd page – detail tableview it will display the item_number that belongs to the category selected. like this ..

Main Tableview:

ABC >
XYZ >

Detail Tableview (selected “ABC”):

123
456

(1) What’s the best approach for this?

(2) I guess my 1st obstacle is parsing the value out from the category “ABC”

    <Product category="ABC">

in my implementation I’m getting blanks when I simply call (somehow parsing a value when there’s a category in the XML is a bit different)

cell.textLabel.text = product.product;

(3) The 2nd is how can I store the data (or can I keep the same approach*), where the detail Tableview page can be referenced by the user selecting a single category from the 1st page.


Current implementation for didStartElement

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
        if ( [elementName isEqualToString:@"Product"] ){
            self.currentProduct = [[Product alloc] init];
            self.storeCharacters = NO;
        } else  if ([elementName isEqualToString:@"Item_Number"]) {
            [self.currentString setString:@""];
            self.storeCharacters = YES;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:00:07+00:00Added an answer on June 15, 2026 at 10:00 pm

    Regarding parsing the category, that’s retrieved from didStartElement from the attributes dictionary when elementName is @"Product".

    In terms of the “best approach”, you just need to decide on your data structure. I might suggest building a NSMutableArray which is an array of category dictionary entries, one per category, and one of the objects in the dictionary would be an array of products in that category.

    Once you have that structure, answering your third question is probably obvious.


    Ok, first, your XML really needs a outer tag, something like:

    <Products>
        <Product category="ABC">
            <Item_number>123</Item_number>
            <Description>Coffee table</Description>
        </Product>
    
        <Product category="ABC">
            <Item_number>456</Item_number>
            <Description>Lamp shade</Description>
        </Product>
    
        <Product category="XYZ">
            <Item_number>789</Item_number>
            <Description>Orange chair</Description>
        </Product>
    </Products>
    

    Second, assuming you wanted an array of categories, and for each category, you wanted an array of products, the implementation might look like the following. First you’d want a few properties, one for the final result and two more for temporary variables used during parsing:

    // this is our final result, an array of dictionaries for each categor
    
    @property (nonatomic, strong) NSMutableArray *categories;
    
    //  these are just temporary variables used during the parsing
    
    @property (nonatomic, strong) NSMutableString *parserElementValue;
    @property (nonatomic, strong) NSMutableDictionary *parserProduct;
    

    And then the NSXMLParserDelegate methods might look like:

    #pragma mark - NSXMLParser delegate methods
    
    - (void)parserDidStartDocument:(NSXMLParser *)parser
    {
        self.categories = [NSMutableArray array];
    }
    
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
        NSArray *subElementNames = @[@"Item_number", @"Description"];
    
        if ([elementName isEqualToString:@"Product"])
        {
            // get the name of the category attribute
    
            NSString *categoryName = [attributeDict objectForKey:@"category"];
            NSAssert(categoryName, @"no category found");
    
            // search our array of dictionaries of cateogries to see if we have one with a name equal to categoryName
    
            __block NSMutableDictionary *parserCurrentCategory = nil;
            [self.categories enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                if ([categoryName isEqualToString:[obj objectForKey:@"name"]])
                {
                    parserCurrentCategory = obj;
                    *stop = YES;
                }
            }];
    
            // if we didn't find one, let's create one and add it to our array of cateogires
    
            if (!parserCurrentCategory)
            {
                parserCurrentCategory = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        categoryName, @"name",
                                        [NSMutableArray array], @"items",
                                        nil];
                [self.categories addObject:parserCurrentCategory];
            }
    
            // Now let's add an entry to the items array for the product being added
    
            self.parserProduct = [NSMutableDictionary dictionary];
            [[parserCurrentCategory objectForKey:@"items"] addObject:self.parserProduct];
        }
        else if ([subElementNames containsObject:elementName])
        {
            self.parserElementValue = [NSMutableString string];
            [self.parserProduct setObject:self.parserElementValue forKey:elementName];
        }
    }
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
        if (self.parserElementValue)
            [self.parserElementValue appendString:string];
    }
    
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {
        if ([elementName isEqualToString:@"Product"])
        {
            self.parserProduct = nil;
        }
        else if (self.parserElementValue)
        {
            self.parserElementValue = nil;
        }
    }
    
    - (void)parserDidEndDocument:(NSXMLParser *)parser
    {
        // all done, do whatever you want, just as reloadData for your table
    
        NSLog(@"%s categories = %@", __FUNCTION__, self.categories);
    }
    
    - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
    {
        NSLog(@"%s error=%@", __FUNCTION__, parseError);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using FluentNHibernate but NHibernate XML will do. Say I have this model public
I am parsing some XML that will have a link such as the following
I have an XML document, several actually, that will be editable via a front-end
I have the following XML code. You will notice the tag Description is repeated,
i have this button xml, when it be will pressed, the pressed image(b) would
What will happen if i have two mirrors with same id in the settings.xml
How to setup(in XML) that the button will have width half of screen. I
I will be doing an occiasional import from XML into core data. I have
I have a button in my XML, that when clicked will toggle the visibility
i will give an example of the problem i have. My XML is like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.