http://blog.sina.com.cn/s/blog_6f40a0e70100srix.html
1.创建NSConnection 对象,设置委托对象
NSMutableURLRequest
*request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self urlString]]];
[NSURLConnection
connectionWithRequest:request delegate:self];
2.
NSURLConnection delegate委托方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connectionDidFinishLoadi ng:(NSURLConnection *)connection;
3.
实现委托方法
-
(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//
store data
[self.receivedData
setLength:0]; //通常在这里先清空接受数据的缓存
}
-
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.receivedData
appendData:data]; //可能多次收到数据,把新的数据添加在现有数据最后
}
-
(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// 错误 处理
}
-
(void)connectionDidFinishLoadi ng:(NSURLConnection
*)connection {
//
disconnect
[UIApplication
sharedApplication].networkActivityIndicator Visible
= NO;
NSString
*returnString = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
NSLog(returnString);
[self
urlLoaded:[self urlString] data:self.receivedData];
firstTimeDownloaded
= YES;
}
三:使用NSXMLParser解析xml文件
1.
设置委托对象,开始解析
NSXMLParser
*parser = [[NSXMLParser alloc] initWithData:data]; //或者也可以使用initWithContentsOfURL直接下载文件,但是有一个原因不这么做:
//
It's also possible to have NSXMLParser download the data, by passing it a URL, but this is not desirable
//
because it gives less control over the network, particularly in responding to connection errors.
[parser
setDelegate:self];
[parser
parse];
2.
常用的委托方法
-
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString
*)namespaceURI
qualifiedName:(NSString
*)qName
attributes:(NSDictionary
*)attributeDict;
-
(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString
*)namespaceURI
qualifiedName:(NSString
*)qName;
-
(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
-
(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;
static
NSString *feedURLString = @"http://www.yifeiyang.net/test/test.xml";
3. 应用举例
-
(void)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error
{
NSXMLParser
*parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
[parser
setDelegate:self];
[parser
setShouldProcessNamespac es:NO];
[parser
setShouldReportNamespace Prefixes:NO];
[parser
setShouldResolveExternal Entities: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:@"user"]) {
//
输出属性值
NSLog(@"Name
is %@ , Age is %@", [attributeDict objectForKey:@"name"], [attributeDict objectForKey:@"age"]);
}
}
-
(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
}
NSError
*parseError = nil;
[self
parseXMLFileAtURL:[NSURL URLWithString:feedURLString] parseError:&parseError];