iphone NSXMLParser解析xml文件

首先,如果要解析xml文件,要实现NSXMLParserDelegate协议

noteXml.h文件

#import <Foundation/Foundation.h>
@class NotePad;
@class NoteDb;
@interface NoteXml : NSObject<NSXMLParserDelegate>{
    NoteDb *noteDb;
    NotePad *newNote;
    //文本节点的内容
    NSMutableString *temString;
    //判断xml是否解析完成
    BOOL isXml;
}
@property(nonatomic,assign)BOOL isXml;
@property(nonatomic,retain)  NoteDb *noteDb;
@property(nonatomic,retain) NotePad *newNote;
@property(nonatomic,retain) NSMutableString *temString;

-(void)parserNote:(NSData*)_data;
@end

noteXml.m文件

#import "NoteXml.h"
#import "NoteDb.h"
#import "NotePad.h"
@implementation NoteXml
@synthesize noteDb,newNote,temString,isXml;
- (id)init {
    self = [super init];
    if (!self) {
        [self release];
        return nil;
    }
    isXml=NO;
    noteDb=[[NoteDb alloc]init];
    newNote=[[NotePad alloc]init];
    temString=[[NSMutableString alloc]init];
    return self;
}
-(void)parserNote:(NSData*)_data{
    //NSXMLParser 实现的是sax方法解析xml文件。
    //设置xml数据,通过NSData创建解析器,还可以通过NSURL创建解析器
    NSXMLParser *parser=[[NSXMLParser alloc]initWithData:_data];
    //回调,代理,委托
    parser.delegate=self;
    //运行解析器
    [parser parse];
}
//文档开始时处理函数
-(void)parserDidStartDocument:(NSXMLParser *)parser{
    [noteDb removeAllNotes];
    [temString setString:@""];
}
//发现元素开始符的处理函数  (即报告元素的开始以及元素的属性) 
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    if ([elementName isEqualToString:@"Note"]) {
        self.newNote=nil;
        self.newNote=[[[NotePad alloc]init]autorelease];
    }
    [temString setString:@""];
}
//处理标签包含内容字符 (报告元素的所有或部分内容)
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if (![string isEqualToString:@""]) {
        [temString appendString:string];
    }
}
//发现元素结束符的处理函数,保存元素各项目数据(即报告元素的结束标记)
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqualToString:@"Note"]) {
        [self.noteDb addNote:newNote];
    }else if([elementName isEqualToString:@"theme"]){
        newNote.theme=[NSMutableString stringWithString:self.temString];
    }else if([elementName isEqualToString:@"information"]){
        newNote.information=[NSMutableString stringWithString:self.temString];
    }else if([elementName isEqualToString:@"date"]){
        newNote.ndate=[NSMutableString stringWithString:self.temString];
    }else if([elementName isEqualToString:@"priority"]){
        NSInteger value=[self.temString integerValue];
        newNote.priority=value;
    }
    
}
//文档结束
-(void)parserDidEndDocument:(NSXMLParser *)parser{
    NSLog(@"%@",self.noteDb);
    self.isXml=YES;
}
- (void)dealloc {
    [noteDb release];
    [newNote release];
    [temString release];
    [super dealloc];
}
@end

ok!本博客是我自己的练习,有好多地方没有讲太清楚,还请谅解!

你可能感兴趣的:(xml,String,iPhone,Class,interface,attributes)