Iphone 解析器读取标记值两次
我试图解析xml文件,但没有得到合适的解析数据。当我在gdb中得到输出时,我从xml文件中的标记中得到的值是原来的两倍。这是我的密码:Iphone 解析器读取标记值两次,iphone,objective-c,ios4,Iphone,Objective C,Ios4,我试图解析xml文件,但没有得到合适的解析数据。当我在gdb中得到输出时,我从xml文件中的标记中得到的值是原来的两倍。这是我的密码: - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attribut
- (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:@"category"];
[item setObject:description forKey:@"description"];
[item setObject:content forKey:@"content:encoded"];
[arr addObject:[item copy]];
// NSLog(@"adding story: %@", currentTitle);
}
}
以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生命医生类……爱住在哪里
2011-08-11 17:35:42.341生命医生类……爱住在哪里
这是我的xml文件:
我希望从item标签中得到值,即title、pubDate、category等,但每次都会打印两次值。请帮助我。请查看位于的文档。具体来说,它说- 解析器对象可以向委托发送几个parser:foundCharacters:messages来报告元素的字符。因为字符串可能只是当前元素的全部字符内容的一部分,所以您应该将其附加到当前字符的累加中,直到元素更改为止 因此,根据粗体文本,多个NSLog不是问题。但是,在您的例子中,参数字符串为null(可能是由于解析错误)。仅当字符串为非null时,才应追加该字符串 嗯,
Akshay < /P> < P>而不是将内容追加到字符串中,应考虑在-< /P>中设置值
(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}
所以代码是这样的
- (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);
}...
}
@Nandkumar:我直接赋值,而不是附加值。但是这些值没有显示在设备中。能否确保只调用一次“parse”方法。