xml文件如下:Question.xml,放在Resource文件目录中
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Question>
<QuestionTitle>你的学习成绩如何?</QuestionTitle>
<Answers>
<Answer>
<Result>主要科目成绩较好</Result>
<Evaluate>对自己的学习成绩一定要如实回答。</Evaluate>
</Answer>
<Answer>
<Result>大概可在中等偏上水平</Result>
<Evaluate>如果成绩不好则应说明理由。</Evaluate>
</Answer>
<Answer>
<Result>成绩特好,在我们班老数第一</Result>
<Evaluate>应表现出对学习的态度是认真的。</Evaluate>
</Answer>
</Answers>
</Question>
</Root>
IVPaserXMLSingleton.h 文件如下
#import <Foundation/Foundation.h>
#import "IVQuestionModel.h"
#import "IVAnswerModel.h"
@interface IVPaserXMLSingleton : NSObject {
NSXMLParser*m_parser; //xml 解析类
IVQuestionModel* m_queModel; //问题的封装类
IVAnswerModel* m_ansModel; //问题答案的封装类
NSMutableArray* m_arrXMLNode; //xml解析后的封装的array列表
}
//实现单利方法
+(IVPaserXMLSingleton *) GetInstance;
//解析xml文件 并将解析后的结果封装到列表中返回
- (NSMutableArray*) GetArrayByPaserXML;
@end
IVPaserXMLSingleton.m 文件如下,为单例类
@implementation IVPaserXMLSingleton
static IVPaserXMLSingleton *instance; //单例对象
BOOL flag = YES; //是否为一个节点 重复的赋值
NSString* m_strCurrentElement; //读到的当前节点的名
+ (IVPaserXMLSingleton *)GetInstance {
@synchronized(self) {
if (instance == nil) {
instance = [[self alloc] init];
}
}
return instance;
}
- (NSMutableArray*) GetArrayByPaserXML {
//获取xml文件
NSString* path = path = [[NSBundle mainBundle] pathForResource:@"Question" ofType:@"xml"];
NSFileHandle* file = [NSFileHandle fileHandleForReadingAtPath:path];
NSData* data = [file readDataToEndOfFile];
[file closeFile];
m_parser = [[NSXMLParser alloc] initWithData:data];
//设置该类本身为代理类
[m_parser setDelegate:self];
BOOL flag = [m_parser parse];
if (flag) {
NSLog(@"获取指定路径的xml文件成功");
} else {
NSLog(@"获取指定路径的xml文件失败");
}
[m_parser release];
return m_arrXMLNode;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary *)attributeDict {
//读到开始标签 flag = YES
flag = YES;
// 纪录当前解析的节点
m_strCurrentElement = elementName;
////读到xml的跟结点的开始标签
if ([elementName isEqualToString:@"Root"]) {
m_arrXMLNode = [[NSMutableArray alloc] init];
}
//解析面试问题的xml文件
if ([m_xmlType isEqualToString:@"Question"]) {
if ([elementName isEqualToString:@"Question"]) {
m_queModel = [[IVQuestionModel alloc] init];
m_queModel.m_arrAnswers = [[NSMutableArray alloc] init];
} else if ([elementName isEqualToString:@"Answer"]) {
m_ansModel = [[IVAnswerModel alloc] init];
}
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (flag) {
//解析面试问题的xml文件
if ([m_xmlType isEqualToString:@"Question"]) {
if ([m_strCurrentElement isEqualToString:@"QuestionTitle"]) {
m_queModel.m_strQuestionTitle = string;
} else if ([m_strCurrentElement isEqualToString:@"Result"]) {
m_ansModel.m_strResult = string;
} else if ([m_strCurrentElement isEqualToString:@"Evaluate"]) {
m_ansModel.m_strEvaluate = string;
}
}
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
//读到结束标签 flag = NO
flag = NO;
//解析面试问题的xml文件
if ([m_xmlType isEqualToString:@"Question"]) {
if ([elementName isEqualToString:@"Question"]) {
[m_arrXMLNode addObject:m_queModel];
[[m_queModel m_arrAnswers] release];
[m_queModel release];
m_queModel = nil;
}
if ([elementName isEqualToString:@"Answer"]) {
[[m_queModel m_arrAnswers] addObject:m_ansModel];
[m_ansModel release];
m_ansModel = nil;
}
}
}
- (void)parserDidStartDocument:(NSXMLParser *)parser {
NSLog(@"开始解析xml文件");
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(@"解析xml文件完成");
}
@end
大致过程就是这样,常遇到的问题:
1。如果xml文件读取失败,可能是xml的文件格式不正确,可以在window下用ie打开该xml文件可以快速的查看是否xml文件的格式有问题。
2。如果节点的值过长,如<Result>...</Result>中间有很长一段内容,那么解析的过程中会将这段内容截成几段来读取,即:-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string方法会被重复的调用,直到所有的内容读取结束,所以这时如果用:m_ansModel.m_strResult = string;这种方式赋值,得到就会是不全的内容,解决办法是用NSMutableString类型的去接收读到的值,大概像这样:
if (m_ansModel.m_strResult == nil) {
m_ansModel.m_strResult = [[NSMutableString alloc] initWithString:string];
} else {
[m_ansModel.m_strResult appendString:string];
}
其中m_strResult为NSMutableString类型。就ok啦