tag value is read by parser twice
I am trying to parse an xml file, but not getting suitable parsed data. When i get output in gdb, than i get twice the value from tags in xml file. here is my code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
// clear out our story item caches...
//item = [[NSMutableDictionary alloc] init];
blogtitle = [[NSMutableString alloc] init];
publishdate = [[NSMutableString alloc] init];
creator = [[NSMutableString alloc] init];
category = [[NSMutableString alloc] init];
description = [[NSMutableString alloc] init];
content = [[NSMutableString alloc] init];
}}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ( [currentElement isEqualToString:@"title"]) {
[blogtitle appendString:string];
NSLog(@"blogtitle....%@",blogtitle);
}else if ([currentElement isEqualToString:@"pubDate"]) {
[publishdate appendString:string];
NSLog(@"publishdate....%@",publishdate);
}else if ([currentElement isEqualToString:@"dc:creator"]) {
[creator appendString:string];
NSLog(@"creator....%@",creator);
}else if ([currentElement isEqualToString:@"category"]) {
[category appendString:string];
NSLog(@"category....%@",category);
} if ([currentElement isEqualToString:@"description"]) {
[description appendString:string];
NSLog(@"description....%@",description);
} if ([currentElement isEqualToString:@"content:encoded"]) {
[content appendString:string];
NSLog(@"content....%@",content);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"item"]) {
// save values to an item, then store that item into the array...
[item setObject:blogtitle forKey:@"title"];
[item setObject:publishdate forKey:@"pubDate"];
[item setObject:creator forKey:@"dc:creator"];
[item setObject:category forKey:开发者_JAVA技巧@"category"];
[item setObject:description forKey:@"description"];
[item setObject:content forKey:@"content:encoded"];
[arr addObject:[item copy]];
// NSLog(@"adding story: %@", currentTitle);
}
}
output in gdb:
2011-08-11 17:32:49.651 the-life-doctor[5282:207] blogtitle....(null)
2011-08-11 17:33:02.897 the-life-doctor[5282:207] blogtitle....(null)
2011-08-11 17:33:07.760 the-life-doctor[5282:207] description....(null)
2011-08-11 17:33:09.217 the-life-doctor[5282:207] description....(null)
2011-08-11 17:33:10.559 the-life-doctor[5282:207] publishdate....(null)
2011-08-11 17:33:11.769 the-life-doctor[5282:207] publishdate....(null)
2011-08-11 17:33:24.104 the-life-doctor[5282:207] blogtitle....WHERE DOES LOVE LIVE
warning: Attempting to create USE_BLOCK_IN_FRAME variable with block that isn't in the frame.
warning: Attempting to create USE_BLOCK_IN_FRAME variable with block that isn't in the frame.
2011-08-11 17:35:34.265 the-life-doctor[5282:207] blogtitle....WHERE DOES LOVE LIVE
2011-08-11 17:35:39.053 the-life-doctor[5282:207] publishdate....Mon, 22 Nov 2010 17:43:47 +0000
2011-08-11 17:35:40.146 the-life-doctor[5282:207] publishdate....Mon, 22 Nov 2010 17:43:47 +0000
2011-08-11 17:35:40.644 the-life-doctor[5282:207] creator....admin
2011-08-11 17:35:41.236 the-life-doctor[5282:207] creator....admin
2011-08-11 17:35:41.898 the-life-doctor[5282:207] category....WHERE DOES LOVE LIVE 2011-08-11 17:35:42.341 the-life-doctor[5282:207] category....WHERE DOES LOVE LIVE
here is my xml file:
i want the values from item tag, i.e title, pubDate, category etc, but each time, the value get printed in twice. please help me out.
Have a look at the documentation at http://developer.apple.com/library/ios/#documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html. Specifically, it says-
The parser object may send the delegate several parser:foundCharacters: messages to report the characters of an element. Because string may be only part of the total character content for the current element, you should append it to the current accumulation of characters until the element changes.
Hence, according to the Bold text, multiple NSLogs aren't a problem. However, in your case, the parameter string is null (perhaps due to a parsing error). You should be appending string only if it is non-null.
HTH,
Akshay
rather than appending content to the string, you should consider setting values in -
(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}
so the code will be like
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ( [currentElement isEqualToString:@"title"]) {
blogtitle = string;
NSLog(@"blogtitle....%@",blogtitle);
}else if ([currentElement isEqualToString:@"pubDate"]) {
publishdate = string;
NSLog(@"publishdate....%@",publishdate);
}...
}
精彩评论