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