IOS study notes (nine) -- XML data analysis

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

    In iPhone development, XML analysis has a lot of choice, iOS SDK provides NSXMLParser and libxml2 two library, there are also many third party libraries available, such as TBXML, TouchXML, KissXML, TinyXML and GDataXML. This article uses the NSXMLParser.


Analysis of XML there are generally two kinds of ways, DOM and SAX:

    NSXMLParser is an analytical method based on SAX. NSXMLParser has used the design pattern, the need to use agreement and support the principal of his. NSXMLParser analysis of XML by NSXMLParserDelegate.

1,The initialization of NSXMLParser


To use NSXMLParser to create it, set up various attributes, mainly using the following methods:

initWithContentsOfURL  Create a parser by NSURL

initWithData

Create a parser by NSData

setDelegate

Commissioned for the parser grammar

parse

Run the parser


   NSXMLParser *parser=[[NSXMLParser alloc] initWithData:[xml dataUsingEncoding:NSUTF8StringEncoding]];
    
//    NSXMLParser *parser=[[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://earthquake.usgs.gov/earthquakes/catalogs/7day-M2.5.xml"]];
    
    [parser setDelegate:self];//The analytical method of the NSXMLParser object is set
    [parser setShouldProcessNamespaces:NO];
    [parser parse];//To parse

2,The delegate method


The main use of the delegate method:


//Discovery processing function element start symbol (attribute to report element and the element) 
- (void)parser:(NSXMLParser *)parser 
        didStartElement:(NSString *)elementName 
        namespaceURI:(NSString *)namespaceURI 
        qualifiedName:(NSString *)qName 
        attributes:(NSDictionary *)attributeDict

//Processing label contains characters (all or part of the contents of the report element) 
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

//Discovery of element end processing function symbol, save the elements of the project data (the end tag that report element)
- (void)parser:(NSXMLParser *)parser 
        didEndElement:(NSString *)elementName 
        namespaceURI:(NSString *)namespaceURI 
        qualifiedName:(NSString *)qName

//End report analysis 
- (void)parserDidEndDocument:(NSXMLParser *)parser

//Parse error report can not be restored
- (void)paser:parserErrorOccured

 

Implementation of [parser parse] are executed in sequence:

1,didStartElement

2,foundCharacters

3,didEndElement

Examples are as follows:


-(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");
    }
    
}

NSXMLParser DEMO Download



/**

* @Author Zhang Xingye

*  

*  Introduction to iOS group: 83702688

*  Android development of advanced group: 241395671

*  My Sina micro-blog: @ Zhang Xingye TBOW

*/

 

Reference resources:

 

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#

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Glendon at November 27, 2013 - 8:55 PM