1、解析本地xml文件
NSString*path = [[NSBundlemainBundle] pathForResource:@"test" ofType:@ "xml" ];
NSFileHandle*file = [NSFileHandlefileHandleForReadingAtPath:path];
NSData*data = [file readDataToEndOfFile];
NSXMLParser* xmlRead = [[NSXMLParseralloc] initWithData:data];
[data release];
[xmlRead setDelegate:self];
[xmlRead parse];
//找到本地test.xml文件
NSString*path = [[NSBundlemainBundle] pathForResource:@"test"ofType:@"xml"];
NSFileHandle*file = [NSFileHandlefileHandleForReadingAtPath:path];
NSData*data = [file readDataToEndOfFile];//得到xml文件
//开始解析
NSXMLParser* xmlRead = [[NSXMLParseralloc] initWithData:data];//初始化NSXMLParser对象
[data release];
[xmlRead setDelegate:self];//设置NSXMLParser对象的解析方法代理
[xmlRead parse];//调用代理解析NSXMLParser对象,看解析是否成功
2、解析网络xml文件
#define URIString @ "http://192.168.247.76:8899/hisyo_cn/test.xml"
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:URIString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15 ];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData=[[NSMutableData data] retain];
}
else
{
NSLog(@"error" );
}
- (void )connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0 ];
}
- (void )connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void )connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
[connection release];
[receivedData release];
NSLog(@"Error" );
[theContent setText:@"Connection defeat" ];
}
- (void )connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *content = [[NSString alloc] initWithData: receivedData encoding: NSUTF8StringEncoding];
NSLog(@"content: %@" ,content);
[theContent setText:content];
xmlRead = [[NSXMLParser alloc] initWithData:receivedData];
[xmlRead setDelegate:self];
[xmlRead parse];
[connection release];
[receivedData release];
}