OC xml

 个人笔记,高手绕道

普通方法解析xml:

.h文件

@property (nonatomic,strong) NSMutableArray *itemData;
@property(nonatomic,assign)NSString *currentTagName;

.m文件

注意@synthesize上面的属性

- (void)getxml{
    NSString *xmlPath=[[NSString alloc] init];
    xmlPath=[[NSBundle mainBundle] pathForResource:@"student" ofType:@"xml"];
    NSString *xmlContent=[[NSString alloc] initWithContentsOfFile:xmlPath encoding:NSUTF8StringEncoding error:nil];
    NSXMLParser *myParse=[[NSXMLParser alloc] initWithData:[xmlContent dataUsingEncoding:NSUTF8StringEncoding]];
    [myParse setDelegate:self];
    [myParse parse];  
    [myParse release];
    
    
    
}

- (void)parserDidStartDocument:(NSXMLParser *)parser{
    itemData=[[NSMutableArray alloc] init];
    NSLog(@"start");
}

- (void)parserDidEndDocument:(NSXMLParser *)parser{
    NSLog(@"end1");
}
//三个关键代理方法
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    currentTagName=elementName;
    if ([currentTagName isEqualToString:@"student"]) {
        NSString *noteId=[attributeDict objectForKey:@"id"];
        NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
        [dict setObject:noteId forKey:@"id"];
        
        [itemData addObject:dict];
    }
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    string=[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([string isEqualToString:@""]) {
        return;
    }
    NSMutableDictionary *dict=[itemData lastObject];
    if ([currentTagName isEqualToString:@"name"]&&dict) {
        [dict setObject:string forKey:@"name"];
        NSLog(@"%@",string);
    }if ([currentTagName isEqualToString:@"chinese"]&&dict) {
        [dict setObject:string forKey:@"chinese"];
        NSLog(@"%@",string);
    }if ([currentTagName isEqualToString:@"math"]&&dict) {
        [dict setObject:string forKey:@"math"];
        NSLog(@"%@",string);
    }if ([currentTagName isEqualToString:@"english"]&&dict) {
        [dict setObject:string forKey:@"english"];
        NSLog(@"%@",string);
    }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//    NSLog(@"end2");
}

通过第三方库TBXML解析xml:

首先需要拿到TBXML库文件:http://www.tbxml.co.uk/TBXML/TBXML.zip   

添加libz.dylib

  note=[[NSMutableArray alloc] init];
   TBXML *tbXml=[TBXML tbxmlWithXMLFile:@"student.xml"];
   TBXMLElement *root=tbXml.rootXMLElement;

    if (root) {
        TBXMLElement *noteElement=[TBXML childElementNamed:@"student" parentElement:root];
        while (noteElement!=nil) {
            NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
            TBXMLElement *nameElement=[TBXML childElementNamed:@"name" parentElement:noteElement];
            if (nameElement!=nil) {
                NSString *name=[TBXML textForElement:nameElement];
                [dict setValue:name forKey:@"name"];
                NSLog(@"%@",name);
            }
            
            TBXMLElement *chineseElement=[TBXML childElementNamed:@"chinese" parentElement:noteElement];
            if (chineseElement!=nil) {
                NSString *chinese=[TBXML textForElement:chineseElement];
                [dict setValue:chinese forKey:@"chinese"];
                NSLog(@"%@",chinese);
            }
            
            TBXMLElement *mathElement=[TBXML childElementNamed:@"math" parentElement:noteElement];
            if (mathElement!=nil) {
                NSString *math=[TBXML textForElement:mathElement];
                [dict setValue:math forKey:@"math"];
                NSLog(@"%@",math);
            }
            
            TBXMLElement *englishElement=[TBXML childElementNamed:@"english" parentElement:noteElement];
            if (englishElement!=nil) {
                NSString *english=[TBXML textForElement:englishElement];
                [dict setValue:english forKey:@"english"];
                NSLog(@"%@",english);
            }
            
            NSString *theID=[TBXML valueOfAttributeNamed:@"id" forElement:noteElement];
            [dict setValue:theID forKey:@"id"];
            [note addObject:dict];
            noteElement=[TBXML nextSiblingNamed:@"student" searchFromElement:noteElement];
        }
    }
    
        
    NSLog(@"endall");

student.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<students>
    
    <student id='1'>
        <name>john</name>
        <chinese> 15 </chinese>
        <english>356</english>
        <math>45</math>
    </student>
    
    <student id='2'>
        <name>jim</name>
        <chinese> 222 </chinese>
        <english>222</english>
        <math>222</math>
    </student>
    
</students>

 

 

 

 

 

转载于:https://www.cnblogs.com/masaka/p/3334661.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值