IOS parses XML and uses dictionaries and variable arrays to store data.
@ Interface withoutbookingview: uiviewcontroller <uitextfielddelegate, nsxmlparserdelegate,
Uitableviewdelegate, uitableviewdatasource> {
@ Private
// * ---------------- Select the time ------------------------*//
Uidatepicker * datepicker _;
//*----------------------------------------------*//
// * --------------- Create an array to store data and tableview to display the selected list ----------------*//
@ Public
// Array
// Campus
Nsarray * schooldistrictlist _;
// Classroom type
Nsmutablearray * classtypemutablearray _;
// Classroom
Nsmutablearray * lessonclassroommutablearray _;
// Save the array of classroom Arrays
Nsmutablearray * alltheclassroommutablearrar _;
// Array Used for display
Nsarray * classtypelist _;
Nsarray * lessonclassroomlist _;
// Store all data dictionaries in XML
Nsdictionary * alltheclassroomdictionary _;
// Times
Nsarray * lessonsectionlist _;
// Create a tableview
// Campus
Uitableview * schooldistricttableview _;
// Classroom type
Uitableview * classtypetableview _;
// Classroom
Uitableview * lessonclassroomtableview _;
// Times
Uitableview * lessonsectiontableview _;
//*---------------------------------------------------------------*//
// *********************************** Parse XML *** ***********************************
Nsdata * xmldata _;
Nsxmlparser * parserxml _;
Nsdata * datatoparse _;
Nsmutablearray * workingarray _;
Nsmutablestring * workingpropertystring _;
Nsarray * elementstoparse _;
Bool storingcharacterdata _;
// *********************************** Parse XML *** ***********************************
}
@ End
-(Void) viewdidload
{
[Super viewdidload];
// * = ========================================================== = *//
// Campus and festival
Schooldistrictlist _ = [[nsarray alloc] initwithobjects: @ "University City", @ "Longdong", @ "Dongfeng road", @ "Business School", nil];
Lessonsectionlist _ = [[nsarray alloc] initwithobjects: @ "1, 2", @ "3, 4", @ "5", @ "6, 7", @ "8, 9", @ "10, 11, 12 ", nil];
// The default value is section 1 and 2.
Lessonsectiontextfield _. Text = [lessonsectionlist _ objectatindex: 0];
// Initialize the classroom type, classroom, and Dictionary
Classtypemutablearray _ = [[nsmutablearray alloc] init];
Lessonclassroommutablearray _ = [[nsmutablearray alloc] init];
// * =================================================== ========================================================== ============= *//
// * ----------------------------------- Initialize tableview -------------------------------------*//
// Unify the size of tableview
Cgrect tableviewframe = cgrectmake (120,120,165,140 );
// Campus
Schooldistricttableview _ = [[uitableview alloc] initwithframe: tableviewframe style: uitableviewstylegrouped];
Schooldistricttableview _. Delegate = self;
Schooldistricttableview _. datasource = self;
// Times
Lessonsectiontableview _ = [[uitableview alloc] initwithframe: cgrectmake (150,220, lessonsectiontextfield _. Frame. Size. Width, 140) style: uitableviewstylegrouped];
Lessonsectiontableview _. Delegate = self;
Lessonsectiontableview _. datasource = self;
Tableviewframe. Origin. Y + = dudaotextfielddistance;
// Classroom type
Classtypetableview _ = [[uitableview alloc] initwithframe: tableviewframe style: uitableviewstylegrouped];
Classtypetableview _. Delegate = self;
Classtypetableview _. datasource = self;
Tableviewframe. Origin. Y + = dudaotextfielddistance;
// Classroom
Lessonclassroomtableview _ = [[uitableview alloc] initwithframe: tableviewframe style: uitableviewstylegrouped];
Lessonclassroomtableview _. Delegate = self;
Lessonclassroomtableview _. datasource = self;
// * Optional //*----------------------------------------------------------------------------------------*//
}
The following is only a method for parsing XML, and other functions are required to start the following method for parsing XML:
# Pragma mark ********************************* Parse XML ** ***********************************
// Traverse XML nodes
-(Void) parser :( nsxmlparser *) parser didstartelement :( nsstring *) elementname namespaceuri :( nsstring *) namespaceuri qualifiedname :( nsstring *) QNAME attributes :( nsdictionary *) attributedict
{
// Determine whether elementname and string-array are equal
If ([elementname isequaltostring: @ "string-array"])
{
[Classtypemutablearray _ addobject: [attributedict objectforkey: @ "name"];
}
// Query whether the specified object exists
Storingcharacterdata _ = [elementstoparse _ containsobject: elementname];
}
// Call this method if the node has a value
-(Void) parser :( nsxmlparser *) parser foundcharacters :( nsstring *) String
{
If (storingcharacterdata _)
{
[Workingpropertystring _ appendstring: String];
// Link a string to the end of another string
}
}
// Enter the end mark
-(Void) parser :( nsxmlparser *) parser didendelement :( nsstring *) elementname namespaceuri :( nsstring *) namespaceuri qualifiedname :( nsstring *) QNAME
{
If (storingcharacterdata _)
{
// Nsstring method, remove spaces before and after the string
Nsstring * trimmedstring = [workingpropertystring _ stringbytrimmingcharactersinset: [nscharacterset whitespaceandnewlinecharacterset];
// Empty the string
[Workingpropertystring _ setstring: @ ""];
If ([elementname isequaltostring: @ "item"])
{
[Lessonclassroommutablearray _ addobject: trimmedstring];
// Nslog (@ "% @", trimmedstring );
}
}
// When string-array is encountered
If ([elementname isequaltostring: @ "string-array"])
{
If ([lessonclassroommutablearray _ count])
{
[Alltheclassroommutablearrar _ addobject: [nsarray arraywithobject: lessonclassroommutablearray _];
// Release the memory first
[Lessonclassroommutablearray _ release];
Lessonclassroommutablearray _ = [[nsmutablearray alloc] init];
}
}
}
//************************************** ********************************//
Here, the corresponding XML file name is selected first, and then the method for parsing XML is called.
# 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 _);
// *********************************** Parse XML *** ***********************************
// Try XML
// Initialization is used to temporarily store strings read from XML
Workingpropertystring _ = [nsmutablestring string];
// Initialization is used to store the parsed XML file
Workingarray _ = [nsmutablearray array];
// Obtain the XML file from the resource file
Nsstring * strpathxml = [[nsbundle mainbundle] pathforresource: schooldistrictname oftype: @ "XML"];
// Convert an XML file to the Data Type
Xmldata _ = [[nsdata alloc] initwithcontentsoffile: strpathxml];
// Initialize the XML to be parsed
Parserxml _ = [[nsxmlparser alloc] initwithdata: xmldata _];
// Initialize the elements to be parsed from XML
Elementstoparse _ = [nsarray arraywithobjects: @ "string-array", @ "item", nil];
// Set the XML parsing proxy to self
[Parserxml _ setdelegate: Self];
// Clear the array before parsing
[Classtypemutablearray _ removeallobjects];
// Initialize and store two-dimensional arrays of all classrooms
Alltheclassroommutablearrar _ = [[nsmutablearray alloc] init];
// Start Parsing
// Jump to the three methods used to parse XML
[Parserxml _ parse]; // call the parsed proxy method
// Store all corresponding classrooms in dictionary form
Alltheclassroomdictionary _ = [[nsdictionary alloc] initwithobjects: alltheclassroommutablearrar _ forkeys: classtypemutablearray _];
// Update 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]; // obtain the corresponding array from the dictionary
// 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;
}
}