Parsing XML files with NSXMLParser:
1. Create an NSXMLParser instance and input XML data received from the server.
2. Define the parser proxy
3 parser.
4. parse XML data using the parsing proxy method.
# Pragma mark load xml-(void) loadXML {// obtain network data. NSLog (@ "load xml"); // load data from the web server NSString * str = @ "http://www.baidu.com? Format = xml "; // This is a mess. // 1 resume NSURL * url = [NSURL URLWithString: str]; // 2 create NSURLRequest * request = [NSURLRequest requestWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 2.0f]; // 3 create a NSURLConnect synchronization method to load data NSURLResponse * response = nil; NSError * error = nil; // synchronously load data NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: & response err Or: & error]; // 1 instantiate the parser and pass in the data NSXMLParser * parser = [[NSXMLParser alloc] initWithData: data]; // 2 set the proxy [parser setDelegate: self]; // 3 parser, this method will call the method in the proxy. [Parser parse];}/* xml parsing idea; 0 Data initialization, instantiate dataList and the global string to be used in step 3. 1. In the second method, elementName = video. It will include videoId in attributeDict. 2. If in the second method, elementName = video .. A Global video attribute needs to be instantiated. Record 2, 3, 4, and parse the current video information object 3. Perform the 2, 3, and 4 methods in sequence for other attributes, at the same time, the third method can be called multiple times. 4. In the third method, You Need To concatenate a string and define a global intermediate process of property records. 5. In the fourth method, you can obtain the content of elementName from the string concatenated by the third method. You can set the value corresponding to elementName of the Global video Object. 6. In method 4, if elementName = video, insert the method into datalist. Preparations: 1. A global string that records the complete content of each element. 2. A global video object that records the elements being parsed. */# Pragma mark-XML parser proxy method // 1 resolution document-(void) parserDidStartDocument :( NSXMLParser *) parser {NSLog (@ "Start Parsing "); // load the 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: @ ""] ;}// parse the entire XML file Before, 2, 3, 4 will be repeatedly called // 2 to start parsing an element, and the new node starts. -(Void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary *) attributeDict {NSLog (@ "Start parsing element % @", elementName, attributeDict); if ([elementName isinclutostring: @ "video"]) {// 1 instantiate currentvideo self. currentVideo = [[Video alloc] init]; // 2 save videoid self. currentVideo. videoId = [attributeD Ict [@ "videoId"] integerValue];} // you need to clear the intermediate string [self. elementString setString: @ ""];} // 3 receives the data of the element. (This method is called repeatedly because the element content is too large. data needs to be spliced) -(void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string {NSLog (@ "found character % @", string); [self. elementString appendString: string];} // 4 end parsing an element-(void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifi EdName :( NSString *) qName {NSLog (@ "Finish parsing element % @", elementName); // obtain the string spliced in step 3. NSString * result = [NSString stringWithString: self. elementString]; if ([elementName isinclutostring: @ "name"]) {self. currentVideo. name = result;} else if ([elementName isinclutostring: @ "length"]) {self. currentVideo. length = [result integerValue];} else if ([elementName isinclutostring: @ "videoURL"]) {self. currentVideo. videoId = [result integerValue];} else if ([elementName isinclutostring: @ "imageURL"]) {self. currentVideo. imageURL = result;} else if ([elementName isinclutostring: @ "desc"]) {self. currentVideo. desc = result;} else if ([elementName isinclutostring: @ "teacher"]) {self. currentVideo. teacher = result;} else if ([elementName isinclutostring: @ "video"]) {[self. dataList addObject: self. currentVideo] ;}// 5 Resolution document end-(void) parserDidEndDocument :( NSXMLParser *) parser {NSLog (@ "document end % @", self. dataList); // clear temporary data self. currentVideo = nil; [self. elementString setString: @ ""]; // uitableview refresh data [self. tableView reloadData];} // 6 parsing error-(void) parser :( NSXMLParser *) parser validationerroccurred :( NSError *) validationError {NSLog (@ "Resolution error "); // clear the temporary data self. currentVideo = nil; [self. elementString setString: @ ""]; // clear the temporary array [self. dataList removeAllObjects];}