On Wed, Nov 12, 2008 at 6:22 PM, Ariel Rodriguez <[EMAIL PROTECTED]> wrote: > thanks Stephen for your kind response. I think i've grasp the theory, but i > am not qite sure about the best way to implement the algorithm. Perhaps to > set a flag to indicate wether the "country" object is the root one or the > nested one?
That's the basic idea. First, draw out the state machine (see the attached PNG). Then you assign numbers to each of the states: typedef enum _MyParserStates { STATE_INITIAL, STATE_EDIT_COUNTRY, STATE_SET_COUNTRY, STATE_SET_ID, STATE_SET_GDP, STATE_SET_ISO_CODE } MyParserStates; Then, you code the transitions in your delegate (which tracks the state). - (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { switch (state) { case STATE_INITIAL: if ([elementName isEqualToString:@"country"]) { // create a new country object and store it somewhere state = STATE_EDIT_COUNTRY; } break; case STATE_EDIT_COUNTRY: if ([elementName isEqualToString:@"country"]) { state = STATE_SET_COUNTRY; } else if ([elementName isEqualToString:@"id"]) { state = STATE_SET_ID; } else if () { // you get the idea } break; } } - (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { switch (state) { case STATE_SET_COUNTRY: // get the current country object // set the country name field break; case STATE_SET_ID: // get the current country object // set the ID field break; // ...so on and so forth for the rest of the properties } } - (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { switch (state) { case STATE_SET_COUNTRY: if ([elementName isEqualToString:@"country"]) state = STATE_EDIT_COUNTRY; break; case STATE_SET_ID: if ([elementName isEqualToString:@"id"]) state = STATE_EDIT_COUNTRY; break; // ... so on and so forth for the rest of the properties case STATE_EDIT_COUNTRY: if ([elementName isEqualToString:@"country"]) { // validate the current country object // store the current country object somewhere // clear the current country object state = STATE_INITIAL; } } } Pretty straight forward and fool proof!
<<attachment: coutry-state-machine.png>>
_______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]