There are 2 ways to parse XML files 1. Document-based2. Event-based (sax) XML file acquisition in 1.IOS The first step is to import the XML into the projectin the Viewcontroller button event, the code is as follows://Get XML full path NSString * Path=[[nsbundle mainbundle] [email protected] "XML file name" oftype:@ "xml"]; //Read file to memory nsdata* data=[nsdata Datawithcontentsoffile:path]; //Create an XML converter from binary data Nsxmlparser * Parser=[[nsxmlparser Alloc]initwithdata:data]; //Set Agent to implement the agent in the corresponding. h file <NSXMLParserDelegate>[parser setdelegate:self]; //Start conversion[Parser parse];Some of the behaviors commonly used in 2.XMLDocument conversion Start-(void) Parserdidstartdocument: (nsxmlparser) parser{}//Document conversion end-(void) Parserdidenddocument: (Nsxmlparser) parser{}//Element start-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI qualifiedname: (NSString *) QName attributes: (Nsdictionary *) attributedict{}//Element end-(void) parser: (NSXMLP Arser *) Parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI qualifiedname: (NSString * ) QName {}//Discovery string-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string{}3.XML parsingparsing for multiple elements for XML format:The XML code is as follows<students><student> <name> Zhang San </name> <sex> men </sex> <age>21</age> </student> </students>(1) class inheritance Nsoject (jcstudent) to create this elementin JCStudent.h//Name@property (strong,nonatomic) nsstring * name;//Gender@property (strong,nonatomic) nsstring * sex;//Age@property (assign,nonatomic) int age; (2)Setting properties in ViewController.h#import "JCStudent.h" @interface viewcontroller:uiviewcontroller<nsxmlparserdelegate>{Nsmutablearray * STUDENTS; Student ArraysBOOL isbz; Element Start FlagNSString * PARNAME; String Contentjcstudent * student; Student Class }(3) inSeveral behaviors of XML in VIEWCONTROLLER.M write code in Viewdidload method //Initialize dynamic array Students=[[nsmutablearray alloc]initwithcapacity:100]; in the "Document conversion start" method //emptying data [students removeallobjects]; //string content emptying [email protected] ""; //flag for no isbz=no; at "element start" Method if (isbz==yes) {//If the flag is yes parName=elementName;/// String content assignment } if ([elementname isequaltostring:@ "Student"]) {//If the element is student & nbsp Isbz=yes; Flag changed to yes student=[[jcstudent Alloc]init]; Jcstudent initialization } in "Discovery string" method if (Isbz==yes && parname.length >0) {//If flag is yes string content has value [Student setvalue:string forkey:parname];//Add string information to attribute class [email protected] "";//string content emptying } in the "element End" method if ([ElementName isequaltostring:@ "Student"]) {//If the element ends with student [Students addobject:student];// Add class to dynamic array isbz=no;//flag set to no } at "End of document conversion" Method /traversal array for (Jcstudent * s in students) { nslog (@ "name=% @,sex=%@,age=%d ", s.name,s.sex,s.age);//console output } For XML format for single element parsing (Attributedict Dictionary):The XML code is as follows<students><student name= "Zhang San" sex= "male" age= "/></students>"(1) class inheritance Nsoject (jcstudent) to create this elementin JCStudent.h//Name@property (strong,nonatomic) nsstring * name;//Gender@property (strong,nonatomic) nsstring * sex;//Age@property (assign,nonatomic) int age; (2)Setting properties in ViewController.h#import "JCStudent.h" @interface viewcontroller:uiviewcontroller<nsxmlparserdelegate>{Nsmutablearray * STUDENTS; Student Arraysjcstudent * student; Student Class }(3) inSeveral behaviors of XML in VIEWCONTROLLER.M write code in the Viewdidload method//Initialize dynamic array Students=[[nsmutablearray alloc]initwithcapacity:100]; In the "Document conversion start" method//emptying the data [students removeallobjects]; In the element start methodif ([elementname isequaltostring:@ "Student"]) {//If the start element is studentstudent= [[Jcstudent alloc]init];//student class initializationNsarray * arr=[attributedict allkeys];//new array get key for all elementsFor (NSString * str in arr) {//traversal array[student setvalue:[attributedict Objectforkey:str] forkey:str];//add array to student class, value is obtained from key } }In the "element End" method if ([ElementName isequaltostring:@ "Student"]) {//If the element ends with student [students addobject:student];//add class to dynamic array } in the "Document Conversion End" method//Traversal array for (Jcstudent * s in students) {NSLog (@ "name=%@,sex=%@,age=%d", S.NAME,S.S Ex,s.age);//Console output}
XML parsing of IOS-database network