IOS Study Notes (9)-xml data parsing

Source: Internet
Author: User

This article uses NSXMLParser.

 

       

       

      NSXMLParser * parser = [[NSXMLParser alloc] initWithData: [xml dataUsingEncoding: encoding]; // NSXMLParser * parser = [[NSXMLParser alloc] parser: [NSURL URLWithString: @ "http://earthquake.usgs.gov/earthquakes/catalogs/7day-M2.5.xml"]; [parser setDelegate: self]; // set the NSXMLParser object's resolution proxy [parser setShouldProcessNamespaces: NO]; [parser parse]; // start Parsing


      2. Delegate Method

       

       

      // The processing function that discovers the element start character (that is, the start of the report element and the attribute of the element)-(void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary *) attributeDict // processing tag contains content characters (all or part of the report element)-(void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string // The processing function that discovers the element Terminator and stores the data of each element Project (that is, the end mark of the report element)-(void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName // report resolution end-(void) Complete :( NSXMLParser *) parser // report unrecoverable parsing errors-(void) paser: parsererroccured

       

      After [parser parse] is executed, it is executed in sequence:

       

      -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {        NSLog(@"xml_parser start %@ ",elementName);    if ( [elementName isEqualToString:@"person"] ) {        NSLog(@"xml_parser person ");        if(personNameArrary==nil){            personNameArrary=[[NSMutableArray alloc] init];        }    }        if(itemValue!=nil){            itemValue=nil;        }        itemValue=[[NSMutableString alloc] init];        if ( [elementName isEqualToString:@"phone"] ) {          NSString *atr=[attributeDict valueForKey:@"type"];        NSLog(@"xml_parser phone type: %@",atr);    }}- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {        NSLog(@"xml_parser value:%@",string);    [itemValue appendString:string];}- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {    NSLog(@"xml_parser end %@ ",elementName);    if ( [elementName isEqualToString:@"firstName"] ) {        [personNameArrary addObject:itemValue];    }        if ( [elementName isEqualToString:@"person"] ) {        NSLog(@"xml_parser person end");    }    }


      Download NSXMLParser DEMO

       

       


      @ Zhang xingye TBOW

      Http://www.cnblogs.com/dotey/archive/2011/05/11/2042000.html

       

      Https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html#//apple_ref/doc/uid/20002265-BCIJFGJI

      Http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project #

      Related Article

      Contact Us

      The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

      If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

      A Free Trial That Lets You Build Big!

      Start building with 50+ products and up to 12 months usage for Elastic Compute Service

      • Sales Support

        1 on 1 presale consultation

      • After-Sales Support

        24/7 Technical Support 6 Free Tickets per Quarter Faster Response

      • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.