NSXmlParser iPhone sdk解析数据问题

发布于 2024-11-26 04:52:19 字数 1525 浏览 0 评论 0原文

我正在解析来自 rss 的一些数据,但其中一些数据正在部分解析,因此尝试附加字符串,但它不起作用,这里是一些代码:

-(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];

 }

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];

 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2024-12-03 04:52:19

如果我正确读取您的代码,那么该行将

 currentNodeContent = [[st stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]mutableCopy];

始终覆盖您在 if (!currentNodeContent) { 行中所做的工作。

因此您的代码可能会正确解析字符串,但无论如何,它总是会被修剪后的字符串覆盖。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文