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


Create a parser by NSData


Commissioned for the parser grammar


Run the parser

   NSXMLParser *parser=[[NSXMLParser alloc] initWithData:[xml dataUsingEncoding:NSUTF8StringEncoding]];
//    NSXMLParser *parser=[[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@""]];
    [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:




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 ");
            personNameArrary=[[NSMutableArray alloc] init];

        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:

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

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