ios 解析xml 並且運用了字典和可變數組存放資料
@interface WithoutBookingView : UIViewController <UITextFieldDelegate,NSXMLParserDelegate,
UITableViewDelegate,UITableViewDataSource>{
@private
//*----------------選擇時間------------------------*//
UIDatePicker* datePicker_;
//*----------------------------------------------*//
//*---------------建立數組存資料、tableView顯示選擇的列表----------------*//
@public
//數組
//校區
NSArray* schoolDistrictList_;
//教室類型
NSMutableArray* classTypeMutableArray_;
//教室
NSMutableArray* lessonClassroomMutableArray_;
//儲存教室數組的數組
NSMutableArray* allTheClassroomMutableArrar_;
//顯示的時候用會array的數組
NSArray* classTypeList_;
NSArray* lessonClassroomList_;
//存xml裡所有資料的字典
NSDictionary* allTheClassroomDictionary_;
//節次
NSArray* lessonSectionList_;
//建立tableview
//校區
UITableView* schoolDistrictTableView_;
//教室類別
UITableView* classTypeTableView_;
//教室
UITableView* lessonClassroomTableView_;
//節次
UITableView* lessonSectionTableView_;
//*---------------------------------------------------------------*//
//*********************************解析xml**************************************
NSData* xmlData_;
NSXMLParser* parserXML_;
NSData* dataToParse_;
NSMutableArray* workingArray_;
NSMutableString* workingPropertyString_;
NSArray* elementsToParse_;
BOOL storingCharacterData_;
//*********************************解析xml**************************************
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
//*===============================初始化數組、列表等==========================================*//
//校區與節次
schoolDistrictList_ = [ [ NSArray alloc ] initWithObjects: @"大學城",@"龍洞",@"東風路",@"商學院" , nil ];
lessonSectionList_ = [ [ NSArray alloc ] initWithObjects: @"1,2",@"3,4",@"5",@"6,7",@"8,9",@"10,11,12", nil ];
//節次預設第1,2節
lessonSectionTextField_.text = [ lessonSectionList_ objectAtIndex: 0 ];
//初始化教室類型、教室、字典
classTypeMutableArray_ = [ [ NSMutableArray alloc ] init ];
lessonClassroomMutableArray_ = [ [ NSMutableArray alloc ] init ];
//*=======================================================================================*//
//*---------------------------------初始化tableview-------------------------------------*//
//統一tableview的大小
CGRect tableViewFrame = CGRectMake( 120 , 120 , 165 , 140 );
//校區
schoolDistrictTableView_ = [ [ UITableView alloc ] initWithFrame: tableViewFrame style: UITableViewStyleGrouped ];
schoolDistrictTableView_.delegate = self;
schoolDistrictTableView_.dataSource = self;
//節次
lessonSectionTableView_ = [ [ UITableView alloc ] initWithFrame: CGRectMake( 150 , 220 , lessonSectionTextField_.frame.size.width , 140 ) style: UITableViewStyleGrouped ];
lessonSectionTableView_.delegate = self;
lessonSectionTableView_.dataSource = self;
tableViewFrame.origin.y += dudaoTextFieldDistance;
//教室類型
classTypeTableView_ = [ [ UITableView alloc ] initWithFrame: tableViewFrame style: UITableViewStyleGrouped ];
classTypeTableView_.delegate = self;
classTypeTableView_.dataSource = self;
tableViewFrame.origin.y += dudaoTextFieldDistance;
//教室
lessonClassroomTableView_ = [ [ UITableView alloc ] initWithFrame: tableViewFrame style: UITableViewStyleGrouped ];
lessonClassroomTableView_.delegate = self;
lessonClassroomTableView_.dataSource = self;
//*----------------------------------------------------------------------------------------*//
}
以下只是解析xml的方法,另外需要其他函數來啟動以下解析xml的方法
#pragma mark *********************************解析xml*************************************
//遍曆xml的節點
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
//判斷elementName與string-array是否相等
if ( [ elementName isEqualToString: @"string-array" ] )
{
[ classTypeMutableArray_ addObject: [ attributeDict objectForKey: @"name" ] ];
}
//查詢指定對象是否存在
storingCharacterData_ = [ elementsToParse_ containsObject: elementName ];
}
//節點有值則調用此方法
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ( storingCharacterData_ )
{
[ workingPropertyString_ appendString: string ];
//把一個字串接在另一個字串的末尾
}
}
//當遇到結束標記時,進入此句
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ( storingCharacterData_ )
{
//NSString的方法,去掉字串前後的空格
NSString* trimmedString = [ workingPropertyString_ stringByTrimmingCharactersInSet: [ NSCharacterSet whitespaceAndNewlineCharacterSet ] ];
//將字串置空
[ workingPropertyString_ setString:@"" ];;
if ( [ elementName isEqualToString: @"item" ] )
{
[ lessonClassroomMutableArray_ addObject: trimmedString ];
// NSLog( @"%@", trimmedString );
}
}
//遇到string-array時
if ( [ elementName isEqualToString: @"string-array" ] )
{
if ( [ lessonClassroomMutableArray_ count ])
{
[ allTheClassroomMutableArrar_ addObject: [ NSArray arrayWithObject: lessonClassroomMutableArray_ ] ];
//先釋放記憶體
[ lessonClassroomMutableArray_ release ];
lessonClassroomMutableArray_ = [ [ NSMutableArray alloc ] init ];
}
}
}
//**********************************************************************//
這裡就是先選擇了對應的xml檔案名稱,接著就開始調用解析xml的方法。
#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
int row = [ indexPath row ];
if ( tableView == schoolDistrictTableView_ )
{
NSString *schoolDistrictName = [ schoolDistrictList_ objectAtIndex: row ];
[ schoolDistrictTableView_ removeFromSuperview ];
schoolDistrictTextField_.text = schoolDistrictName;
schoolNumber_ = schoolDistrictTextField_.text ;
schoolNumber_ = [ self getDistrictNum ];
// NSLog( @"%@", schoolNumber_ );
//*********************************解析xml**************************************
//嘗試xml
//初始化用來臨時儲存從xml中讀取的字串
workingPropertyString_ = [ NSMutableString string ];
//初始化用來儲存解析後的xml檔案
workingArray_ = [ NSMutableArray array ];
//從資源檔中擷取xml檔案
NSString *strPathXml = [[NSBundle mainBundle] pathForResource: schoolDistrictName ofType:@"xml"];
//將xml檔案轉換成data類型
xmlData_ = [ [ NSData alloc ] initWithContentsOfFile: strPathXml ];
//初始化待解析的xml
parserXML_ = [ [ NSXMLParser alloc ] initWithData: xmlData_ ];
//初始化需要從xml中解析的元素
elementsToParse_ = [ NSArray arrayWithObjects:@"string-array",@"item", nil ];
//設定xml解析代理為self
[ parserXML_ setDelegate: self ];
//開始解析之前清空數組
[ classTypeMutableArray_ removeAllObjects ];
//初始化存放所有課室二維數組
allTheClassroomMutableArrar_ = [ [ NSMutableArray alloc ] init ];
//開始解析
//這裡就跳到上面所用到解析xml的三個方法
[ parserXML_ parse ];//調用解析的代理方法
//以字典形式存放對應的所有課室
allTheClassroomDictionary_ = [ [ NSDictionary alloc ] initWithObjects: allTheClassroomMutableArrar_ forKeys: classTypeMutableArray_ ];
//更新tableview
[ classTypeTableView_ reloadData ];
// NSLog( @"%@",allTheClassroomDictionary_ );
}
else if( tableView == classTypeTableView_ )
{
NSString *classTypeName = [ classTypeMutableArray_ objectAtIndex: row ];
[ classTypeTableView_ removeFromSuperview ];
classTypeTextField_.text = classTypeName;
// NSLog( @"%@",[ allTheClassroomDictionary_ objectForKey: classTypeName ] );
[ lessonClassroomMutableArray_ removeAllObjects ];
[ lessonClassroomMutableArray_ addObject: [ allTheClassroomDictionary_ objectForKey: classTypeName]]; //從字典中擷取對應的數組
// NSLog( @"%@",lessonClassroomMutableArray_ );
// NSLog( @"%@", [ [ lessonClassroomMutableArray_ objectAtIndex: 0 ] objectAtIndex: 0 ]);
[ lessonClassroomTableView_ reloadData ];
}
else if( tableView == lessonClassroomTableView_ )
{
NSString *lessonclassroomName = [ [ [ lessonClassroomMutableArray_ objectAtIndex: 0 ] objectAtIndex: 0 ] objectAtIndex: row ];
[ lessonClassroomTableView_ removeFromSuperview ];
lessonClassroomTextField_.text = lessonclassroomName;
}
else
{
NSString *lessonSectionlist = [ lessonSectionList_ objectAtIndex: row ];
[ lessonSectionTableView_ removeFromSuperview];
lessonSectionTextField_.text = lessonSectionlist;
}
}