解析XML檔案樣本.
代碼1和代碼2是IOS開發的基本內容。
代碼1.
#import <UIKit/UIKit.h>
@class XmlTestViewController;
@interface XmlTestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
XmlTestViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet XmlTestViewController *viewController;
@end
代碼2
1 #import "XmlTestAppDelegate.h"
2 #import "XmlTestViewController.h"
3 @implementation XmlTestAppDelegate
4 @synthesize window;
5 @synthesize viewController;
6 #pragma mark -
7 #pragma mark Application lifecycle
8 - (BOOL)application:(UIApplication *)application
9 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
10 [window addSubview:viewController.view];
11 [window makeKeyAndVisible];
12 return YES;
13 }
14 - (void)applicationWillResignActive:(UIApplication *)application {}
15 - (void)applicationDidEnterBackground:(UIApplication *)application {}
16 - (void)applicationWillEnterForeground:(UIApplication *)application {}
17 - (void)applicationDidBecomeActive:(UIApplication *)application {}
18 - (void)applicationWillTerminate:(UIApplication *)application {}
19 - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {}
20 - (void)dealloc {
21 [viewController release];
22 [window release];
23 [super dealloc];
24 }
25 @end
<BR><BR>
代碼3是controller的標頭檔。
代碼3
#import <UIKit/UIKit.h>
@interface XmlTestViewController : UIViewController {
}
@end
controller的實現檔案。在這聲明了一個xml格式,NSString類型的字串。並用代碼5和代碼6定義的類解析。
代碼4
#import "XmlTestViewController.h"
#import "FirstXmlParse.h"
@implementation XmlTestViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *xml=@"<xml>"
"<title>標題</title>"
"<content>內容描述</content>"
"<time>2011-1-1</time>"
"</xml>";
FirstXmlParse *first=[[FirstXmlParse alloc] init];
[first startParse:xml];
NSLog(@"title:%@",first.title);
NSLog(@"content:%@",first.content);
NSLog(@"time:%@",first.time);
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
代碼5
#import <Foundation/Foundation.h>
@interface FirstXmlParse : NSObject<NSXMLParserDelegate> {
NSString *title;
NSString *content;
NSString *time;
//定義一個可變字串
NSMutableString *contentString;
}
@property(nonatomic,retain)NSString *title;
@property(nonatomic,retain)NSString *content;
@property(nonatomic,retain)NSString *time;
@property(nonatomic,retain)NSMutableString *contentString;
-(void)startParse:(NSString *)xml;
@end
代碼6.這是解析xml檔案的關鍵內容。
#import "FirstXmlParse.h"
@implementation FirstXmlParse
@synthesize title;
@synthesize content;
@synthesize time;
@synthesize contentString;
-(void)startParse:(NSString *)xml{
NSData *data=[xml dataUsingEncoding:NSUTF8StringEncoding];
//建立xml解析器
NSXMLParser *xmlParse=[[NSXMLParser alloc] initWithData:data];
//設定委託
[xmlParse setDelegate:self];
[xmlParse parse];
}
//解析文檔開始
- (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@"解析文檔開始");
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
NSLog(@"遇到啟始標籤:%@",elementName);
self.contentString=[NSMutableString string];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@"遇到內容:%@",string);
[self.contentString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@"遇到結束標籤:%@",elementName);
if ([elementName isEqualToString:@"title"]) {
title=contentString;
}
if ([elementName isEqualToString:@"content"]) {
content=contentString;
}
if ([elementName isEqualToString:@"time"]) {
time=contentString;
}
}
//解析文檔結束
- (void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@"文檔解析結束");
}
@end
摘自 生於凜冽,葬於北邙