views:

46

answers:

1

Hi all,

so here I am stuck at the simplest part (or so I thought) of my small project for this morning. The goal was to build a simple XML Parser who takes every element they encounter and add it to a dictionary that will eventually hold all key/value pairs of the xml.

So an xml like this

<xml>
  <1stTag>text</1stTag>
  <2ndTag>some more text</2ndTag>
</xml>

Would end up in a dictionary with 2 key/value pairs:

{
1stTag:text;
2ndTag:some more text;
}

Adopting the NSXMLParser Framework I thought that I'd just store every encountered element and texts in variables self.elementInProgress and self.textInProgress (both NSStrings). Once the Element is finished I'd add the pair to the dictionary.

So here is the .h file:

    @interface MSParser : NSObject <NSXMLParserDelegate> 
    {
        NSString *elementInProgress;
        NSString *textInProgress;
        NSMutableDictionary *parsedXMLDict;
    }
@property (nonatomic, retain) NSString *textInProgress;
@property (nonatomic, retain) NSString *elementInProgress;
@property (nonatomic, retain) NSMutableDictionary *parsedXMLDict;

And the respective function in the implementation file:

@synthesize textInProgress, elementInProgress, parsedXMLDict;

- (void)parser:(NSXMLParser *)parser  didEndElement:(NSString *)elementName  namespaceURI:(NSString *)namespaceURI  qualifiedName:(NSString *)qName
{
    [self.parsedXMLDict setValue:self.textInProgress forKey:self.elementInProgress];
    NSLog(@"%@",[self.parsedXMLDict valueForKey:self.elementInProgress]);
}

Now, here I am, stumped that my NSLog returns Null! And I thought this was a no-brainer. What am I missing? Both self.textInProgress and self.elementInProgress are filled correctly according to the Debugger. I can trace it down to this line where I call setValue ForKey, which again is according to documentation and has worked more than once before for me. The dictionary just doesnt take the values. According to documentation, it would only act funny, if the passed setValue was nil, which here it isn't... Any ideas?
It all looks good, not compiler errors or warnings, it just returns Null. Stumped, as I said...

+1  A: 

Somewhere in your code (probably in your init method) you need to be instantiating a NSMutableDictionary instance and assigning it to your property (or ivar, if you're in your init method). Something like this:

parsedXMLDict = [[NSMutableDictionary alloc] init];

If you're using a convenience construction that returns an autoreleased object, you'll also need a retain.

parsedXMLDict = [[NSMutableDictionary dictionaryWithCapacity:10] retain];

In either case, make sure you have a corresponding release somewhere, probably in your dealloc.

If you're already doing the above, then perhaps you've already released parsedXMLDict earlier in your code? Look for places where you're calling self.parsedXMLDict = nil that are getting called before your parser: didEndElement: ... method.

Robot K
That was it. thanks a mil! I should have seen that one myself. Oh well.. ;-) Thanks again
karlmeier