iphone解析XML文件

//初始化工作

- (void)Down

{

NSURL* url = [NSURL URLWithString:@"http://192.168.67.3:8080/movie/movie.xml"];

    

NSURLRequest* request = [[NSURLRequest alloc]initWithURL:url];

    

NSURLConnection* connection = [[NSURLConnection alloc]initWithRequest:request 

                                                                 delegate:self];

    

    NSLog(@"--------------------dow");

[connection release];

[request release];

}



//从网络上下载的数据,直到数据全部下载完成 通过data获得请求后,返回的数据,数据类型NSData

- (void)connection:(NSURLConnection *)connection 

didReceiveData:(NSMutableData *)data

{

    NSLog(@"-------------------connect");

[buffer appendData:data];

}


//http交互正常,完成。数据请求完毕,这个时候,用法是多线程的时候,通过这个通知,关部子线程  

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSLog(@"-------------------connect2");

    //[[NSFileManager defaultManager]createFileAtPath:@"/tmp/movie.txt" contents:buffer attributes:nil];

    

[self PraxMovie:buffer];

//[[NSFileManager defaultManager]createFileAtPath:@"/tmp/movie.xml" contents:buffer attributes:nil];

}


//网络连接不成功,出现异常。

- (void)connection:(NSURLConnection *)connection 

  didFailWithError:(NSError *)error

{

    NSLog(@"-------------------error");

UIAlertView *errorAlert = [[UIAlertView alloc]

  initWithTitle: [error localizedDescription]

  message: [error localizedFailureReason]

  delegate:nil

  cancelButtonTitle:@"OK"

  otherButtonTitles:nil];

[errorAlert show];

[errorAlert release];

}

- (void)PraxMovie:(NSData*)_data 

{

    NSLog(@"-------------------praxmovie");

    

//NSData* xmlData = [NSData dataWithContentsOfFile:@"/tmp/movie.xml"];

NSXMLParser* parser = [[NSXMLParser alloc]initWithData:_data];

parser.delegate = self;

[parser parse];

}

//文档处理方法 开始解析前,在这里可以做一些初始化工作

- (void)parserDidStartDocument:(NSXMLParser *)parser

{

    NSLog(@"-------------------parserDidStartDocument");

[xmlMovielist.moviearray removeAllObjects];

[tempString setString:@""];

}

//当解析器对象遇到xml的开始标记时,调用这个方法。

//获得结点头的值


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 

  namespaceURI:(NSString *)namespaceURI 

 qualifiedName:(NSString *)qualifiedName

attributes:(NSDictionary *)attributeDict

{

    NSLog(@"-------------------parser");

if([elementName isEqualToString:@"Movie"])

{

self.newwMovie = nil;

self.newwMovie= [[Movie alloc] init];

[tempString setString:@""];

}

else

{

[tempString setString:@""];

}

}

//当解析器找到开始标记和结束标记之间的字符时,调用这个方法。

//解析器,从两个结点之间读取具体内容

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{

    NSLog(@"-------------------parseraaaaaa");

if(![string isEqualToString:@""])

{

[tempString appendString:string];

}

}


//当解析器对象遇到xml的结束标记时,调用这个方法。

//获取结点结尾的值,此处为一Tag的完成

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 

  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

{

    NSLog(@"-------------------hehe");

if([elementName isEqualToString:@"Movie"])

{

[self.xmlMovielist Addmovie:self.newwMovie];

        

}

else if([elementName isEqualToString:@"title"])

{

self.newwMovie.title =[NSString stringWithString:tempString];

        

}

else if([elementName isEqualToString:@"box"])

{

int temp = [[NSString stringWithString:tempString]intValue];

self.newwMovie.boxOfficeGross = [NSNumber numberWithInt:temp];

}

else if([elementName isEqualToString:@"summary"])

{

self.newwMovie.summary =[NSString stringWithString:tempString];

}

}

//xml解析结束后的一些操作可在此

- (void)parserDidEndDocument:(NSXMLParser *)parser

{

    for (id tmp in self.xmlMovielist.moviearray)

    {

        NSLog(@"tmp = %@",tmp);

    }

//self.newwMovie = nil;

//self.tempString = nil;

//[newMovie  release];

//[tempString release];

}





- (void)dealloc 

{

[xmlMovielist release];

[tempString release];

[NSMutableData release];

    [super dealloc];

}

你可能感兴趣的:(iphone解析XML文件)