2011-05-06 15:09:35| 分类: 解析 | 标签: |字号大中小 订阅
//开始对返回的data进行解析————
-(void)startTwitterParser
{
NSXMLParser *parser =[[NSXMLParser alloc]initWithData:twitterData];
parser.delegate = self;
[parser parse];
[parser release];
}
#pragma mark NSXMLParserDelegate--->
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
twitterDic= [[NSMutableDictionary alloc] initWithCapacity:0];//每一条信息都用字典来存;
parserObjects= [[NSMutableArrayalloc]init];//每一组信息都用数组来存,做后得到的数据就在这个数组中
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"item"]) //找节点进行解析
{
NSMutableDictionary *newNode = [[NSMutableDictionary alloc]initWithCapacity:0];
[parserObjectsaddObject:newNode];
twitterDic = newNode;
}
elseif(twitterDic)
{
if ([elementName isEqualToString:@"enclosure"])
{
//NSLog(@"%@",attributeDict);//存到字典下了
if ([[attributeDict valueForKey:@"url"] hasSuffix:@"jpg"]) //把encolsuer下的节点放在字典中
{
[twitterDicsetObject:[attributeDict valueForKey:@"url"] forKey:@"image"];
}
else
{
[twitterDicsetObject:[attributeDict valueForKey:@"url"] forKey:@"video"];
}
}
else
{
NSMutableString *string = [[NSMutableString alloc]initWithCapacity:0];
[twitterDicsetObject:string forKey:elementName];
[string release];
currentText = [[NSStringalloc] init];
currentElementName = elementName;
}
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentText= [[NSString alloc] initWithString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:currentElementName])
{
[twitterDic setObject:currentText forKey:currentElementName];//把其他节点放在字典中
}
}
-(void)parserDidEndDocument:(NSXMLParser *)parser//得到的解析结果
{
NSLog(@"%d %@",[parserObjects count],[[parserObjects objectAtIndex:0] valueForKey:@"title"]);
}
#pragma mark NSXMLParserDelegate end----->
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"item"]) //找节点进行解析
{
NSMutableDictionary *newNode = [[NSMutableDictionaryalloc] initWithCapacity:0];
[parserObjects addObject:newNode];
twitterDic = newNode;
}
elseif (twitterDic)
{
if ([elementName isEqualToString:@"enclosure"])
{
//NSLog(@"%@",attributeDict);//存到字典下了
if ([[attributeDict valueForKey:@"url"] hasSuffix:@"jpg"]) //把encolsuer下的节点放在字典中
{
[twitterDic setObject:[attributeDict valueForKey:@"url"] forKey:@"image"];
}
else
{
[twitterDic setObject:[attributeDict valueForKey:@"url"] forKey:@"video"];
}
}
else
{
NSMutableString *string = [[NSMutableStringalloc] initWithCapacity:0];
[twitterDic setObject:string forKey:elementName];
[string release];
currentText = [[NSString alloc] init];
currentElementName = elementName;
}
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentText = [[NSStringalloc] initWithString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:currentElementName])
{
[twitterDicsetObject:currentTextforKey:currentElementName];//把其他节点放在字典中
}
}
-(void)parserDidEndDocument:(NSXMLParser *)parser//得到的解析结果
{
NSLog(@"%d %@",[parserObjectscount],[[parserObjectsobjectAtIndex:0] valueForKey:@"title"]);
}
通过分析xml内容的格式来看整个XML 是以<item>为节点的20条信息内容,而且每个<item>节点下有这着不同的信息,其中有个信息 是<enolsure>,它的下面又有几条信息。按照这样的格式分析,写下了以上代码。大家在写代码的时候要根据自己的需求来写的。通过不停 的触发这些代理方法,直到XML被解析完毕,20条信息就存在数组parserObjects里面了,而且每条信息都以字典的形式存在。于是一个我们可以 随时用的内容就生成了,上面最后一个代理方法的输出就是:20 和title下对应的字符串