NSXmlParser iPhone sdk解析数据问题
我正在解析来自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确读取您的代码,那么该行将
始终覆盖您在
if (!currentNodeContent) {
行中所做的工作。因此您的代码可能会正确解析字符串,但无论如何,它总是会被修剪后的字符串覆盖。
If I am reading your code correctly then the line
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.