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 6771507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:26:25+00:00 2026-05-26T15:26:25+00:00

i’m having some troubles trying to parse an XML file and display the results

  • 0

i’m having some troubles trying to parse an XML file and display the results grouped by an object property on a UITableView.
This is the XML file, I can change the structure if it’s necessary:

<Anuncios>
   <Anuncio id="1">
      <localeName>name1</localeName>
      <address>address1</address>
      <type>A</type>
   </Anuncio>
   <Anuncio id="2">
      <localeName>name2</localeName>
      <address>address2</address>
      <type>B</type>
   </Anuncio>
   <Anuncio id="3">
      <localeName>name3</localeName>
      <address>address3</address>
      <type>A</type>
   </Anuncio>
</Anuncios>

At this moment I have a NSMutableArray called servs containing objects (Anuncio).

This is the structure:

@interface Anuncio : NSObject

NSInteger iden;
NSString *localeName;
NSString *address;
NSString *type;

So, I would like to sort the UITableView by anuncio.type with the correct titleForHeaderInSection.

I downloaded the TableViewSuite from the Apple documentation, and the second example, SimpleSectionedTableView seems to do what I want, but instead of getting the data from an XML file, it gets the data calling [NSTimeZone knownTimeZoneNames], so the structure is quite different, I tried to adapt the sample code to my project, but i’m starting to realize that seems to be not the best way to do it. So I’m a bit stuck.

I was thinking on having arrays inside the main array containing the objects editing the XML, like that:

servs: {
         arrayA : { anuncio1, anuncio3 }
         arrayB : { anuncio2 }
}

but then I don’t know how to complete the tableView methods.

Here it is the XMLParse code:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    if([elementName isEqualToString:@"Anuncios"]) {
        //Initialize the array.
        appDelegate.servs = [[NSMutableArray alloc] init];
    }
    else if([elementName isEqualToString:@"Anuncio"]) {

        //Initialize the anuncio.
        serv = [[Anuncio alloc] init];

        //Extract the attribute here.
        serv.iden = [[attributeDict objectForKey:@"id"] integerValue];

        NSLog(@"Reading id value: %i", serv.iden);
    }
    NSLog(@"Processing Element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

    if(!currentElementValue) 
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
        [currentElementValue appendString:string];

    NSLog(@"Processing Value: %@", currentElementValue);

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if([elementName isEqualToString:@"Anuncios"])
        return;

    if([elementName isEqualToString:@"Anuncio"]) {
        [appDelegate.servs addObject:serv];

        [serv release];
        serv = nil;
    }
    else 
        [serv setValue:currentElementValue forKey:elementName];

    [currentElementValue release];
    currentElementValue = nil;
}

Part of my current UITableViewController:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [appDelegate.servs count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    anuncios = (NSMutableArray *)[appDelegate.servs sortedArrayUsingSelector:@selector(compare:)];
    Anuncio *anuncio = [anuncios objectAtIndex:section];
    return [anuncio type];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    anuncios = (NSMutableArray *)[appDelegate.servs sortedArrayUsingSelector:@selector(compare:)];
    Anuncio *anuncio = [anuncios objectAtIndex:indexPath.section];

    cell.textLabel.text = anuncio.localeName;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

At the moment I get:

A
--------
name1
--------
A
--------
name3
--------
B
--------
name2

and I would like to get:

A
--------
name1
name3
--------
B
--------
name2

I don’t know if the model that I’m proposing is the right one or i’m missing something. I thought it was something very common but I didn’t find anything about this.
Thanks for helping

  • 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-05-26T15:26:26+00:00Added an answer on May 26, 2026 at 3:26 pm

    In your Anuncio class, add a compare: method like this:

    - (NSComparisonResult) compare:(Anuncio *)otherObject {
        return [self.type compare:otherObject.type];
    }
    

    Then sort your NSMutableArray with this:

    anuncios = (NSMutableArray *)[anuncios sortedArrayUsingSelector:@selector(compare:)];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to parse a xhtml file and display in UITableView. what is the
In my XML file chapters tag has more chapter tag.i need to display chapters
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an XML file, the creators of it stuck in a bunch social

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.