标签:blog io ar os 使用 sp for strong on
<?xml version="1.0" encoding="UTF-8"?>
<root>
<person id="1">
<firstName>zhang</firstName>
<lastName>san</lastName>
<age>51</age>
</person>
<person id="2">
<firstName>li</firstName>
<lastName>si</lastName>
<age>61</age>
</person>
</root>
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate,NSXMLParserDelegate> @property (strong, nonatomic) UIWindow *window; @property (nonatomic, strong) NSXMLParser *xmlParser; @end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//找到文件路径
NSString *xmlFilePath = [[NSBundle mainBundle]pathForResource:@"MyXML" ofType:@"xml"];
NSData *xml = [[NSData alloc]initWithContentsOfFile:xmlFilePath];
self.xmlParser = [[NSXMLParser alloc]initWithData:xml];
self.xmlParser.delegate = self;
if ([self.xmlParser parse]) {
NSLog(@"The XML is parsed");
}else{
NSLog(@"Failed to parse the XML");
}
return YES;
}
为了解析 XML 文件,我们需要了解定义在 NSXMLParserDelegate 协议中的代理方法和它们的职责:
parser:didStartElement:namespaceURI:qualifiedName:attributes:
在 XML document 中,当解析器在解析的时候遇到了一个新的 element 时会被调用该方法。
parser:didEndElement:namespaceURI:qualifiedName:
当前节点结束之后会调用。
parser:foundCharacters:
当解析器在解析文档内容的时候被调用。
#import <Foundation/Foundation.h> @interface XMLElement : NSObject @property (nonatomic, strong)NSString *name; @property (nonatomic, strong)NSString *text; @property (nonatomic, strong)NSDictionary *attributes; @property (nonatomic, strong)NSMutableArray *subElements; @property (nonatomic, weak) XMLElement *parent; @end
.m
#import "XMLElement.h"
@implementation XMLElement
- (NSMutableArray *)subElements{
//get方法 懒加载
if (_subElements == nil) {
_subElements = [[NSMutableArray alloc]init];
}
return _subElements;
}
@end
#import <UIKit/UIKit.h> @class XMLElement; @interface AppDelegate : UIResponder <UIApplicationDelegate,NSXMLParserDelegate> @property (strong, nonatomic) UIWindow *window; @property (nonatomic, strong) NSXMLParser *xmlParser; @property (nonatomic, strong) XMLElement *rootElement; @property (nonatomic, strong) XMLElement *currentElementPointer; @end
//开始解析,重置
- (void)parserDidStartDocument:(NSXMLParser *)parser{
self.rootElement = nil;
self.currentElementPointer = nil;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if (self.rootElement == nil) {
self.rootElement = [[XMLElement alloc]init];
self.currentElementPointer = self.rootElement;
}else{
XMLElement *newElement = [[XMLElement alloc]init];
newElement.parent = self.currentElementPointer;
[self.currentElementPointer.subElements addObject:newElement];
self.currentElementPointer = newElement;
}
self.currentElementPointer.name = elementName;
self.currentElementPointer.attributes = attributeDict;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([self.currentElementPointer.text length] > 0) {
self.currentElementPointer.text = [self.currentElementPointer.text stringByAppendingString:string];
}else{
self.currentElementPointer.text = string;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
self.currentElementPointer = self.currentElementPointer.parent;
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
self.currentElementPointer = nil;
}
标签:blog io ar os 使用 sp for strong on
原文地址:http://www.cnblogs.com/safiri/p/4155442.html