Skip to content

Loading…

trying to parse deeper nested XML #5

Closed
briomusic opened this Issue · 3 comments

2 participants

@briomusic

Hi, I hope this is an ok place to ask this question. So far, I have only used functions from the sample.xml that I adapted to my needs, but I have now come across a stumbling block. How do I parse the following file with SMXMLDocument? (mark up signs replaced with brackets as they wouldn't display in the preview)

(?xml version="1.0" encoding="UTF-8"?)
(results totalResults="74")
(gigs)
(gig)
(venue)A-Trane, Berlin(/venue)
(date)22/10/11(/date)
(merchitems)
(merchitem)
(articleDescription)CD Optativo(/articleDescription)
(articlePrice)5.99(/articlePrice)
(articleSold)4(/articleSold)
(/merchitem)
(merchitem)
(articleDescription)CD Mi Libro Abierto(/articleDescription)
(articlePrice)6.99(/articlePrice)
(articleSold)45(/articleSold)
(/merchitem)
(/merchitems)
(/gig)
(/gigs)
(/results)

I have managed to extract the 'venue' and 'date' items, but am stuck parsing the contained array 'merchitems' (which in itself contains a dictionary) into my iOS project. This is my attempt so far:

SMXMLDocument *document = [SMXMLDocument documentWithData:data error:NULL];
SMXMLElement *gigs = [document.root childNamed:@"gigs"];

// parsing loop
for (SMXMLElement *gig in [gigs childrenNamed:@"gig"]) {

    NSString *venue = [gig valueWithPath:@"venue"]; 
    NSLog(@"currently parsing venue %@", venue);
    NSMutableDictionary *gigReport = [NSMutableDictionary dictionaryWithObject:venue forKey:@"Venue"];

    NSString *date = [gig valueWithPath:@"date"]; 
    [gigReport setObject:date forKey:@"Date"];

    // everything works up to here, then it goes wrong //

    SMXMLElement *merchitems = [document.root descendantWithPath:@"merchitems"];        
    NSMutableArray *merchandiseArray = [NSMutableArray array];
    for (SMXMLElement *merchitem in [merchitems childrenNamed:@"merchitem"]) {

        NSString *articleDesc = [merchitem valueWithPath:@"articleDescription"]; 
        NSLog(@"currently parsing article %@", articleDesc);

        NSMutableDictionary *merchDict = [NSMutableDictionary dictionaryWithObject:articleDesc forKey:@"Article Description"];

        NSString *articlePric = [merchitem valueWithPath:@"articlePrice"];  
        [merchDict setObject:[NSNumber numberWithDouble:[articlePric doubleValue]] forKey:@"Article Price"];

        NSString *articleSol = [merchitem valueWithPath:@"articleSold"]; 
        [merchDict setObject:[NSNumber numberWithInt:[articleSol intValue]] forKey:@"Articles Sold"];

        [merchandiseArray addObject:merchDict];
    }
    [gigReport setObject:merchandiseArray forKey:@"Merchandise Form"];

    [self.storedGigReports addObject:gigReport];
}

Thanks in advance for any help, hopefully one day I can give some back.
briomusic

@nfarina
Owner

I think instead of:

    SMXMLElement *merchitems = [document.root descendantWithPath:@"merchitems"];        

You really want:

    SMXMLElement *merchitems = [gig childNamed:@"merchitems"];        
@briomusic
@nfarina
Owner

Glad to help! And FYI, descendantWithPath is really designed for situations where you want to dig deeper into the hierarchy, which you can do by adding periods. For instance:

NSString *venue = [document.root descendantWithPath:@"gigs.gig.venue"].value;

(But that example is only useful if you have just one <gig>).

@nfarina nfarina closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.