IOS XML Parsing Learning

Source: Internet
Author: User

XML file:

<?xml version= "1.0" encoding= "UTF-8"? ><notes><note id= "1" ><CDate>2013-6-11</CDate> <content>ksdjfaljdfasf</content><userid>alex</userid></note><note id= "2" > <cdate>2012-6-11</cdate><content>vvvvvvvvvvv</content><userid>alex11</userid ></Note></Notes>

//

NotesXMLParser.h

Xml

//

Created by Alex on 13-6-12.

Copyright (c) year Alex. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface Notesxmlparser: NSObject<nsxmlparserdelegate>

@property (nonatomic,strong) nsmutablearray *notes;

@property (nonatomic, strong) nsstring *currenttagname;

-(void) start;

@end

//

Notesxmlparser.m

Xml

//

Created by Alex on 13-6-12.

Copyright (c) year Alex. All rights reserved.

//

#import "NotesXMLParser.h"

@implementation Notesxmlparser

-(void) Start

{

nsstring* Path = [[nsbundle mainbundle] pathforresource: /c6>@ "notes" ofType:@ "xml"];

nsurl *url = [nsurl fileurlwithpath:p Ath];

// start parsing XML

nsxmlparser *parser = [[nsxmlparser alloc] initwithcontentsofurl: url];

Parser. delegate = self;

[Parser parse];

NSLog(@ " parse complete ...");

}

// triggers when the document starts

-(void) Parserdidstartdocument: (nsxmlparser *) parser

{

_notes = [nsmutablearray new];

}

// document is triggered when error occurs

-(void) parser: (nsxmlparser *) Parser parseerroroccurred: (nserror *) parseerror

{

NSLog (@ "%@", parseerror);

}

// trigger 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 = [nsmutabledictionary new];

[Dict setobject: _id forkey:@ "id"];

[_notes addobject:d ICT];

}

}

// trigger when string is encountered

-(void) parser: (nsxmlparser *) Parser foundcharacters: (nsstring *) string

{

string =[string stringbytrimmingcharactersinset: [nscharacterset  Whitespaceandnewlinecharacterset]]; // replace carriage return and spaces

if ([String isequaltostring:@ "]) {

Return

}

nsmutabledictionary *dict = [_notes lastobject];

if ([_currenttagname isequaltostring:@ "CDate"] && dict) {

[Dict setobject: String forkey:@ "CDate"];

}

if ([_currenttagname isequaltostring:@ "Content"] && dict) {

[Dict setobject: String forkey:@ "Content"];

}

if ([_currenttagname isequaltostring:@ "UserID"] && dict) {

[Dict setobject: String forkey:@ "UserID"];

}

}

// start when the end tag is encountered

-(void) parser: (nsxmlparser *) Parser didendelement: (nsstring *) elementname

NamespaceURI: (nsstring *) NamespaceURI

QualifiedName: (nsstring *) qName;

{

self. Currenttagname = nil;

}

// trigger at end of document

-(void) Parserdidenddocument: (nsxmlparser *) parser

{

[[nsnotificationcenter defaultcenter] postnotificationname: @ "Reloadviewnotification" Object:self. Notes userInfo:nil];

Self. notes = nil;

}

@end

#import "ViewController.h"

#import "NotesXMLParser.h"

@interface viewcontroller ()

@end

@implementation Viewcontroller

-(void) viewdidload

{

[super viewdidload];

[[nsnotificationcenter defaultcenter] addobserver: Self selector:@selector(reloadview:) Name:@ "reloadviewnotification" object:nil];

notesxmlparser *parser=[notesxmlparser new];

[Parser start];

}

This article reprinted http://www.dahuangphone.com/dv_rss.asp?s=xhtml&boardid=8&id=133&page=3

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.