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