开发者

NSXmlParser iPhone sdk parsesing data problem [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For 开发者_StackOverflow社区help making this question more broadly applicable, visit the help center. Closed 10 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜