NSXMLParser 学习使用

IOS SDK 的NSXMLParse解析XML文档是时间驱动模式的,即采用SAX方法来解析XML格式文档,NSXMLParse在处理XML文档的过程中,当遇到一些要素(元素,属性,CDATA块,评论等)时会通知它的委托,而自身不对解析的要素进行任何处理,全权委托给NSXMLParserDalegate处理。同时它也会报告错误。所以在使用时要添加代理方法,遵守代理协议

1、打开一个XML文件。读取内容到NSData中,

在viewDidLoad中:

NSXMLParser* parse=[[NSXMLParser alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"test" ofType:@"xml"]]];

    parse.delegate=self;//设置代理

    [parse parse];//执行解析方法

2、实现代理协议的方法,开始解方法

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict

{

    

    if ([elementName isEqualToString:@"question"]) {//查找到节点question

        //将解析的节点存到model中并添加在数组中

        [questions addObject:currentQuestion];

        currentQuestion.question=[attributeDict objectForKey:@"text"];

        currentQuestion.right=[attributeDict objectForKey:@"right"];

    }else if ([elementName isEqualToString:@"answer"])//解析位answer的节点

    {

        NSString* tag=[attributeDict objectForKey:@"tag"];

        if ([tag isEqualToString:@"A"]) {

            currentQuestion.answerA=[attributeDict objectForKey:@"text"];

        }

        else if ([tag isEqualToString:@"B"])

        {

            currentQuestion.answerB=[attributeDict objectForKey:@"text"];

        }

        else if([tag isEqualToString:@"C"])

        {

            currentQuestion.answerC=[attributeDict objectForKey:@"text"];

        }

        else

        {

            currentQuestion.answerD=[attributeDict objectForKey:@"text"];

        }

    }

}

3、实现代理协议方法,解析完成所要执行的方法

-(void)parserDidEndDocument:(NSXMLParser *)parser

{

    NSLog(@"Size Of Array %d",[questions count]);

    Question* q=[questions objectAtIndex:0];

    self.lQuestion.text=q.question;

    self.lAnswerA.text=q.answerA;

    self.lAnswerC.text=q.answerC;

    self.lAnswerB.text=q.answerB;

    self.lAnswerD.text=q.answerD;

    

  

}

4、还有一个代理协议方法,每解析完一组就执行一次,和didStartElement那个方法配对

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

{

    

    //currentQuestion=[[Question alloc] init];

    //[questions addObject:currentQuestion];

}


5、所用的xml文件

<questions>

    <question text="1+1=?" right="C">

        <answer text="1" tag="A"></answer>

        <answer text="3" tag="B"></answer>

        <answer text="2" tag="C"></answer>

        <answer text="4" tag="D"></answer>

    </question>

    <question text="1+2=?" right="C">

        <answer text="1" tag="A"></answer>

        <answer text="3" tag="B"></answer>

        <answer text="2" tag="C"></answer>

        <answer text="4" tag="D"></answer>

    </question>

    <question text="1+3=?" right="D">

        <answer text="1" tag="A"></answer>

        <answer text="3" tag="B"></answer>

        <answer text="2" tag="C"></answer>

        <answer text="4" tag="D"></answer>

    </question>

</questions>


6、.h文件

#import <UIKit/UIKit.h>

#import "Question.h"


@interface ViewController : UIViewController<NSXMLParserDelegate>

{

    NSMutableArray* questions;

    Question* currentQuestion;

}

@property (strong, nonatomic) IBOutlet UILabel *lQuestion;

@property (strong, nonatomic) IBOutlet UILabel *lAnswerA;

@property (strong, nonatomic) IBOutlet UILabel *lAnswerB;

@property (strong, nonatomic) IBOutlet UILabel *lAnswerC;

@property (strong, nonatomic) IBOutlet UILabel *lAnswerD;

@property (strong, nonatomic) IBOutlet UITextField *tfInputAnswer;

@property (strong, nonatomic) IBOutlet UIButton *btnSubmit;

- (IBAction)submitBtn:(id)sender;


@end




Question.h文件

#import <Foundation/Foundation.h>


@interface Question : NSObject


@property(nonatomic,copy)NSString* question;

@property(nonatomic,copy)NSString* right;

@property(nonatomic,copy)NSString* answerA;

@property(nonatomic,copy)NSString* answerB;

@property(nonatomic,copy)NSString* answerC;

@property(nonatomic,copy)NSString* answerD;


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

先行者-阿佰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值