trying to parse deeper nested XML #5

Closed
briomusic opened this Issue Nov 29, 2011 · 3 comments

Comments

Projects
None yet
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

This comment has been minimized.

Show comment
Hide comment
@nfarina

nfarina Nov 29, 2011

Owner

I think instead of:

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

You really want:

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

nfarina commented Nov 29, 2011

I think instead of:

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

You really want:

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

This comment has been minimized.

Show comment
Hide comment
@briomusic

briomusic Nov 29, 2011

that did the trick, whole app is alive again :)

you, sir, are a gentleman and a scholar.
cheers
brio

p.s. feel free to close issue as you please.

On Tue, Nov 29, 2011 at 7:56 PM, Nick Farina <
reply@reply.github.com

wrote:

I think instead of:

   SMXMLElement *merchitems = [document.root descendantWithPath:@

"merchitems"];

You really want:

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

Reply to this email directly or view it on GitHub:
#5 (comment)

that did the trick, whole app is alive again :)

you, sir, are a gentleman and a scholar.
cheers
brio

p.s. feel free to close issue as you please.

On Tue, Nov 29, 2011 at 7:56 PM, Nick Farina <
reply@reply.github.com

wrote:

I think instead of:

   SMXMLElement *merchitems = [document.root descendantWithPath:@

"merchitems"];

You really want:

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

Reply to this email directly or view it on GitHub:
#5 (comment)

@nfarina

This comment has been minimized.

Show comment
Hide comment
@nfarina

nfarina Nov 29, 2011

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>).

Owner

nfarina commented Nov 29, 2011

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 Nov 29, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment