解析xml

方案一:

#import "xmlViewController.h"

@implementation xmlViewController


static NSString *feedURLString = @"http://headlines.yahoo.co.jp/rss/sci.xml";

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
// 解析开始时的处理
}

- (void)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error
{
NSXMLParser
*parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
NSError
*parseError = [parser parserError];
if (parseError && error) {
*error = parseError;
}
[parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
// 元素开始句柄
if (qName) {
elementName
= qName;
}
if ([elementName isEqualToString:@"PicInfoListResult"]) {
// 输出属性值
//NSLog(@"Name is %@", [attributeDict objectForKey:@"Success"]);//,[attributeDict objectForKey:@"RecordCount"]);


}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
// 元素终了句柄
if (qName) {
elementName
= qName;
}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
// 取得元素的text
NSLog(@"%@",string);
}

- (void)viewDidLoad{
NSError
*parseError = nil;
[self parseXMLFileAtURL:[NSURL URLWithString:feedURLString] parseError:
&parseError];

[super viewDidLoad];
}

- (void)dealloc {
[super dealloc];
}

@end

  

方案二:

#import "xml2ViewController.h"

@implementation xml2ViewController

+(void) parseTest {
// 取得获取Yahoo新闻标题的RSS
NSString *urlStr = @"http://headlines.yahoo.co.jp/rss/sci.xml";
// 使用NSXMLParser三部曲:生成、设置代理和开始解析
NSXMLParser *parser = [[NSXMLParser alloc]
initWithContentsOfURL:[NSURL URLWithString:urlStr]];
xml2ViewController
*sp = [[xml2ViewController alloc]init];

[parser setDelegate:sp];
[parser parse];
NSError
*parseError = [parser parserError];
if (parseError ) {
NSLog(
@"Err %@",[parseError description]);
}
NSLog(
@"parseEnd");
}

- (void)viewDidLoad{
[xml2ViewController parseTest];

[super viewDidLoad];
}

-(id)init {
if (self = [super init]) {
tagPath
= [[[NSMutableArray alloc]init]retain];
tagPathAttributs
= [[[NSMutableArray alloc]init]retain];
[tagPath addObject:
@""];
[tagPathAttributs addObject:[NSDictionary dictionary]];
}
return self;
}
-(void)dealloc {
[tagPath release];
[tagPathAttributs release];
[super dealloc];
}

// 获取Tag的全路径
-(NSString*)tagFullPath {
return [tagPath componentsJoinedByString:@"/"];
}

// 开始解析
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString
*)elementName
namespaceURI:(NSString
*)namespaceURI
qualifiedName:(NSString
*)qName
attributes:(NSDictionary
*)attributeDict
{
NSString
*tag = [elementName copy];
[tagPath addObject:tag];
[tagPathAttributs addObject:[attributeDict copy]];

//开始获取文字列
NSString *tagFullPath = [self tagFullPath];
NSLog(
@"%@",tagFullPath);
if ([tagFullPath hasPrefix:@"/PicInfoListResult/Success"]) {
recordingText
= [[[NSMutableString alloc]init] autorelease];
[recordingText appendString:
@""];
NSLog(
@"%@",recordingText);
}
}

// Tag的完成
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString
*)namespaceURI qualifiedName:(NSString *)qName {
//结束获取文字列
NSString *tagFullPath = [self tagFullPath];
if ([tagFullPath hasPrefix:@"/PicInfoListResult/Success"]) {
NSLog(
@"Title:%@",recordingText);
recordingText
= nil;
}

[tagPath removeLastObject];
[tagPathAttributs removeLastObject];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
// 记录所取得的文字列
if (recordingText!=nil) {
[recordingText appendString:
string];
}
NSLog(
@"%@",string);
}
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock{
//NSLog(@"cData:%@",[NSString stringWithUTF8String:[CDATABlock bytes]]);
}

@end

  

posted @ 2011-09-04 11:33  威尚  阅读(852)  评论(0编辑  收藏  举报