Parse the XML file process with Nsxmlparser:
1 Create the Nsxmlparser instance and pass in the incoming XML data from the server.
2 Defining the parser agent
3 parser parsing.
4 parse the XML data by parsing the proxy method.
#pragma mark load xml-(void) loadxml{//Get network data. NSLog (@ "load xml"); Load data from the Web server nsstring *str = @ "Http://www.baidu.com?format=xml"; Here is the Scribble//1 cv nsurl nsurl *url = [Nsurl urlwithstring:str]; 2 Establish nsurlrequest nsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy: Nsurlrequestuseprotocolcachepolicy timeoutinterval:2.0f]; 3 Establish Nsurlconnect synchronization method to load data nsurlresponse *response = nil; Nserror *error = nil; Synchronous load Data NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:& ERROR]; 1 instantiating the parser, passing in the data to be parsed nsxmlparser *parser = [[Nsxmlparser alloc]initwithdata:data]; 2 Setting up the agent [parser setdelegate:self]; 3 parser parsing, this method will invoke the method inside the proxy. [Parser parse];} /* XML parsing ideas; 0 data initialization work, instantiate DataList and the third step need to use the global string. 1 if in the second method, elementname = = video. Will contain videoid 2 in Attributedict if in the second method, elementname = = video. A global video property needs to be instantiated, 2 3 4 steps are parsed to resolve the current visual information Object 3 The other properties are executed in turn, 2, 3, 4, and may be called multiple times in the third method. 4 in a third method, you need toStitching a string requires defining a global property record intermediate procedure 5 in the fourth method, you can get the corresponding content of elementname by a string that is stitched up by the third method. You can set the value of the elementname for the global video object. 6 in the fourth method, if elementname = = Video, the method is inserted into the DataList. Required Preparation: 1 global strings that record the full contents of each element. A 2 Global video object that records the element that is currently being parsed. */#pragma mark-xml parser Proxy method//1 parse document-(void) Parserdidstartdocument: (Nsxmlparser *) parser{NSLog (@ "start parsing"); Lazy Loading instantiated data if (self.datalist = = nil) {self.datalist = [Nsmutablearray array]; }else{[Self.datalist removeallobjects]; }//Intermediate string if (self.elementstring = = nil) {self.elementstring = [nsmutablestring string]; }else{//self.elementstring = @ ""; [Self.elementstring setstring:@ ""]; }}//before the entire XML file parsing is complete, 2,3,4 is called repeatedly//2 begins parsing an element, and the new node begins. -(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict{NSLog (@ "Start parsing element%@%@", ElementName, Attribut EDICT); if ([ElementName Isequaltostring:@ "Video"]) {//1 instantiation Currentvideo self.currentvideo = [[Video Alloc]init]; 2 save VideoID self.currentVideo.videoId = [attributedict[@ "VideoID"]integervalue]; }//need to empty the brokered string [self.elementstring setstring:@ "];} 3 receive the data of the element (discover the character, this method will be repeated because the element content is too large, this method will be called repeatedly, need to splice data)-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string{NSLog (@ "Discovery character%@", string); [Self.elementstring appendstring:string];} 4 end parsing of an element-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI qualifiedname: (NSString *) qname{NSLog (@ "Complete parsing element%@", elementname); Get the string stitched out in the third step. NSString *result = [NSString stringWithString:self.elementString]; if ([elementname isequaltostring:@ "name"]) {self.currentVideo.name = result; }else if ([elementname isequaltostring:@ "Length"]) {self.currentVideo.length = [result IntegerValue]; }else if ([elementname isequaltostring:@ "Videourl"]) {SELF.CUrrentvideo.videoid = [result IntegerValue]; }else if ([elementname isequaltostring:@ "ImageURL"]) {Self.currentVideo.imageURL = result; }else if ([elementname isequaltostring:@ "desc"]) {Self.currentVideo.desc = result; }else if ([elementname isequaltostring:@ "Teacher"]) {self.currentVideo.teacher = result; }else if ([ElementName isequaltostring:@ "video"]) {[Self.datalist addObject:self.currentVideo]; }}//5 Parsing document End-(void) Parserdidenddocument: (Nsxmlparser *) parser{NSLog (@ "Document End%@", self.datalist); Emptying temporary data Self.currentvideo = nil; [Self.elementstring setstring:@ ""]; UITableView Refresh data [Self.tableview reloaddata];} 6 Parse Error-(void) Parser: (Nsxmlparser *) parser validationerroroccurred: (Nserror *) validationerror{NSLog (@ "Parse error"); Emptying temporary data Self.currentvideo = nil; [Self.elementstring setstring:@ ""]; emptying the temporary array [self.datalist removeallobjects];}