IOS parsing xml--using Nsxml

Source: Internet
Author: User

First, the function of parsing document order triggering

1.parserDidStartDocument, triggered at the time of the document.

2.parser:didstartelement:namespaceuri:qualifiedname:attributes, which is triggered when a start tag is encountered, where the NamespaceURI part is the namespace, QualifiedName is a qualified name, and attributes is a collection of properties for the dictionary type.

3.parser:foundcharacters, the string encountered is triggered.

4.parser:didendelement:namespaceuri:qualifiedname, triggered when an end tag is encountered.

5.parserDidEndDocument, triggered at the end of the document.

Second, Nsxmlparser is the analytic class, it has 3 constructs the method

1.initWithContentsOfURL, you can use a URL object to create a resolution object, in this case, the method is loaded from the resource file to obtain the URL object, and then use the URL object to build the Parse object.

2.initWithData, you can create a parse object using NSData.

3.initWithStream, you can create a parse object using an IO stream object.

After the parse object is created, you need to specify the delegate Property object delegate to self, and then send the parse message to begin parsing the document.

Third, the Code

NotesXMLParser.h

#import <Foundation/Foundation.h>@interface Notesxmlparser:nsobject < Nsxmlparserdelegate>// parsed data inside is dictionary type @property (strong,nonatomic) Nsmutablearray *notes; // the name of the current label @property (strong,nonatomic) NSString *currenttagname; // Start parsing -(void) start; @end

Notesxmlparser.m

#import "NotesXMLParser.h"@implementationNotesxmlparser-(void) start{NSString* Path = [[NSBundle mainbundle] Pathforresource:@"Notes"OfType:@"XML"]; Nsurl*url =[Nsurl Fileurlwithpath:path]; //Start parsing XMLNsxmlparser *parser =[[Nsxmlparser alloc] initwithcontentsofurl:url]; Parser.Delegate=Self ;    [Parser parse]; NSLog (@"Parse Complete ...");}//triggers when the document starts- (void) Parserdidstartdocument: (Nsxmlparser *) parser{_notes= [NsmutablearrayNew];}//trigger when the document is in error- (void) Parser: (Nsxmlparser *) parser parseerroroccurred: (Nserror *) parseerror{NSLog (@"%@", parseerror);}//triggered when a start tag is encountered- (void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString*) NamespaceURI qualifiedname: (NSString*) QualifiedName attributes: (nsdictionary*) attributedict{_currenttagname=elementname; if([_currenttagname isequaltostring:@"Note"]) {nsstring*_id = [Attributedict objectforkey:@"ID"]; Nsmutabledictionary*dict = [NsmutabledictionaryNew]; [Dict setobject:_id Forkey:@"ID"];    [_notes addobject:dict]; }    }//triggered when a string is encountered- (void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *)string{    //Replace carriage return and spaces    string=[stringStringbytrimmingcharactersinset:[nscharacterset Whitespaceandnewlinecharacterset]]; if([stringIsequaltostring:@""]) {        return; } nsmutabledictionary*dict =[_notes Lastobject]; if([_currenttagname isequaltostring:@"CDate"] &&dict) {[Dict setobject:stringForkey:@"CDate"]; }        if([_currenttagname isequaltostring:@"Content"] &&dict) {[Dict setobject:stringForkey:@"Content"]; }        if([_currenttagname isequaltostring:@"UserID"] &&dict) {[Dict setobject:stringForkey:@"UserID"]; }}//departure When the end tag is met- (void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString*) NamespaceURI qualifiedname: (NSString*) QName; {Self.currenttagname=Nil;}//triggered at the end of a document encounter- (void) Parserdidenddocument: (Nsxmlparser *) parser{[[Nsnotificationcenter Defaultcenter] Postnotificationname:@"reloadviewnotification" Object: Self.notes Userinfo:nil]; Self.notes=Nil;}@end

IOS parsing xml--using Nsxml

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.