Reputation: 49
I have the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<Message>
<Text>Town park travel</Text>
<Theme>Ready for racing?</Theme>
<Activate_date>2011-03-08</Activate_date>
<Login>Simon</Login>
<Name>Simon </Name>
<Longitude>46.00339</Longitude>
<Latitude>51.515381</Latitude>
</Message>
How can I get elements To NSMutableDictionary?
i tried
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"Message"]){
listOfFriends = [[NSMutableDictionary alloc] init];
if ([elementName isEqualToString:@"text"]) {
[listOfFriends setObject: elementName forKey @"text" ];
}
}
But it is wrong
Now i use same code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"Message"]){
listOfFriends = [[NSMutableDictionary alloc] init];
} else if ([elementName isEqualToString:@"Text"] || [elementName isEqualToString:@"Theme"] || [elementName isEqualToString:@"Activate_date"] || [elementName isEqualToString:@"Login"] || [elementName isEqualToString:@"Longitude"] || [elementName isEqualToString:@"Latitude"]) {
[currentElement setString:@""];
storingCharacters = YES;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"Message"]) {
[listOfFriends release];
} else if ([elementName isEqualToString:@"Text"]) {
txt.text = currentElement;
} else if ([elementName isEqualToString:@"Theme"]) {
theme.text = currentElement;
} else if ([elementName isEqualToString:@"Activate_date"]) {
aDate.text = currentElement;
} else if ([elementName isEqualToString:@"Login"]) {
autor.text = currentElement;
}
storingCharacters = NO;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (storingCharacters) [currentElement appendString:string];
}
But i have empty Lables on view...
Upvotes: 2
Views: 1917
Reputation: 13622
NSXMLParser is stream-based, and sends various messages to its delegate when it encounters particular components in the XML stream - an opening tag, text, an end tag, and so forth. So, create an instance of NSXMLParser, and assign to it a delegate that you write that conforms to the NSXMLParserDelegate protocol. (Edit: From the code you added to your question, it looks like you may have already done this part, or at least made a good start on it.)
In your delegate class' -parser:didStartElement:...
method, check the element name. If it's "Message", create a new NSMutableDictionary to hold the information in its child elements, and store that in a property: (Edit: In your code, it looks like you've declared the instance variable, but you should use dot-syntax to access it as a property, to make sure the correct memory-management happens.)
self.listOfFriends = [NSMutableDictionary dictionary];
For other element names, create an empty NSMutableString that will be used to hold text, and store that in a property:
self.thisString = [NSMutableString string];
In your -parser:foundCharacters:
method, append the string to the mutable string property you created earlier.
[self.thisString appendString:string];
Finally, in your -parser:didEndElement:...
method, check the element name. If it's "Message," you're done with this message element. If your XML stream has only one Message element (as in the example), you can now print the dictionary you've built, or whatever else it is you wanted to do with it. If there can be multiple Message elements, you could add the just-completed one to an array.
If the just-ended element is not a Message, add it to the dictionary with -setValue:forKey:
passing the element name as a key, and the built-up string you created while receiving earlier -parser:foundCharacters:
messages as the value.
[self.listOfFriends setValue:self.thisString forKey:elementName];
Upvotes: 5