NSXmlParser iPhone sdk parsesing data problem [closed]
I have am parsing some data from rss but some of that data is being parsed partially so tried to append string but it is not working here is some code:
-(id)loadXmlByURL:(NSString *)url{
titles = [[NSMutableArray alloc]init];
NSURL *URL = [NSURL URLWithString:url];
parser = [[NSXMLParser alloc]initWithContentsOfURL:URL];
parser.delegate = self;
[parser parse];
return self;
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *) elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *) attributeDict{
if ([elementName isEqualToString:@"item"])
{
currentTitle = [Titles alloc];
titles = [[NSMutableArray alloc]init];
}
}
-(void)parser:(NSXMLParser *) parser didEndElement:(NSString *)elementName
namespaceURI: (NSString *) namespaceURI qualifiedName:(NSString *) qName
{
if ([elementName isEqualToString:@"title"]) {
currentTitle.title = currentNodeContent;
NSLog(@"%@",currentNodeContent);
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string{
NSMutableString *st = [string mutableCopy];
if (!currentNodeContent) {
// init the ad hoc string with the value
currentNodeContent = [[NSMutableString alloc] initWithString:string];
} else {
// append value to the ad hoc string
[currentNodeContent appendString:string
];
}
currentNodeContent = [[st stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]mutableCopy];
}
If I am reading your code correctly then the line
currentNodeContent = [[st stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]mutableCopy];
will always overwrite the work you are doing in the if (!currentNodeContent) {
line.
so your code may be parsing the string correctly, but regardless, it always overrwites with the trimmed string.
精彩评论