views:

38

answers:

2

I'm using NSXMLParser in my RootViewController.m file.

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:response_data];
[xmlParser setDelegate:self];
[xmlParser parse];
[xmlParser release];

I'm also implementing this method to add entries to a dictionary defined in RootViewController.m for later use:

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

However, I'd like to get more than one XML file and do different things with its nodes; this sounds like I need to use external files as delegates. My question is:

If I have the following implementation files (& their header files):

  • RootViewController.m
  • XMLDelegate1.m
  • XMLDelegate2.m

and set the ith NSXMLParser delegate to be XMLDelegatei.m, how can i get those files to return an NSDictionary that I can then add to the NSDictionary defined in RootViewController.m?

I guess there are two possible answers:

  1. Use a method that I don't know about; or
  2. Use a better workflow

I suspect it's 2, but hope it's 1.

Thanks, Andy

UPDATE: So I've found a way of achieving the goal, although I'm not convinced it's the nicest way. Basically, I get my delegate to update a Plist file and use the Notification Center to discover when this happens.

A: 

Not a code example response but a pattern one, if I understand your question:

You might can just define a class, make instances of it, make matching instances of the parser, then manually set the instances of the parser to use the instances of the class as the delegate... Each of those class instances could append it's DOM to a known global array.

That's one way. programmatically setting delegates (not using IB) sounds like what you want.

James Robey
The problem I had with with making some sort of XMLController.m class that handles all the XML parsing is that the didStartElement method doesn't return anything, especially not to the original call from my RootViewController.m.. is there a way around this?
Andy
+1  A: 

There is a pretty good discussion of this "problem" in Apple's Event-Driven XML Programming Guide The section on "Using Multiple Delegates" touches on a strategy to use. The document includes code listing for the Element class which is where the 'smarts' of the algorithm live. In the code example, the author assumes that the reader knows that the sample is for an Element class that has parent, name, attribute, children and parser properties. The Element class implements the NSXMLParserDelegate protocol. The Element class also will have an NSMutableString *content property that is updated in a method - (void)appendString:(NSString *)aString method that is not included in the sample.

falconcreek