【IOS中XML解析】NSXMLParser方法解析XML文件案例

IOS 中的XML解析

 

首先说一下,在IOS中XML解析和在Android中的SAX或者Pull解析差别不大,都是基于事件的解析方式。

 

首先,定义一个对象来表示XML的文档结构

XMLElement.h

 

#import <Foundation/Foundation.h>

 

@interface XMLElement : NSObject

// 标签名称

@property (nonatomic,strongNSString *name;

// 标签的文本(注意在上一级标签和下一级标签之间室友文本的)

@property (nonatomic,strongNSString *text;

// 标签的属性

@property (nonatomic,strongNSDictionary *attributes;

// 子标签集合

@property (nonatomic,strongNSMutableArray *subElements;

// 上一级标签

@property (nonatomic,strongXMLElement *parent;

 

@end

 

XMLElement.m

 

#import "XMLElement.h"

 

@implementation XMLElement

 

@synthesize name,text,attributes,subElements,parent;

 

-(NSMutableArray *) subElements

{

if(subElements == nil){

subElements = [[NSMutableArray alloc]init];

}

return subElements;

}

 

@end

 

XML文件的内容放在/Supporting File/的Group下面。名称为person.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<root>

<person id="1">

<firstName>zhang</firstName>

<lastName>sansan</lastName>

<age>21</age>

</person>

<person id="2">

<firstName>li</firstName>

<lastName>sisisi</lastName>

<age>31</age>

</person>

</root>

 

解析的ViewController.h

 

#import <UIKit/UIKit.h>

#import "XMLElement.h"

 

@interface ViewController : UIViewController <NSXMLParserDelegate>

// 解析器对象

@property (nonatomic,strongNSXMLParser *parser;

// 根元素

@property (nonatomic,strongXMLElement *rootElement;

// 当前的元素

@property (nonatomic,strongXMLElement *currentElementPointer;

 

@end

 

解析的ViewController.m

 

#import "ViewController.h"

#import "UIColor+ColorWithARGB.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if(self != nil){

self.title = @"XML解析";

}

return self;

}

 

- (void)viewDidLoad

{

[super viewDidLoad];

 

self.view.backgroundColor = [UIColor r:0xA5 g:0xA5 b:0x52];

 

 

NSString *xmlFilePath = [[NSBundle mainBundle]pathForResource:@"person"ofType:@"xml"];

NSData *data = [[NSData alloc]initWithContentsOfFile:xmlFilePath];

 

self.parser = [[NSXMLParser alloc]initWithData:data];

self.parser.delegate = self;

 

if([self.parser parse]){

NSLog(@"The XML is Parsed");

NSMutableString *str = [[NSMutableString alloc]init];

[str appendFormat:@"%@\n",self.rootElement.name];

NSMutableArray *subs = self.rootElement.subElements;

for(int i = 0;i < [subs count];i++){

XMLElement *personElement = [subs objectAtIndex:i];

[str appendFormat:@"\tname:%@",personElement.name];

[str appendFormat:@",text:%@\n",personElement.text];

NSArray *subPersonElements = personElement.subElements;

for(int j = 0;j < [subPersonElements count];j++){

XMLElement *subElement = [subPersonElements objectAtIndex:j];

[str appendFormat:@"\t\tname:%@",subElement.name];

[str appendFormat:@",text:%@\n",subElement.text];

}

}

 

NSLog(@"======解析结果:%@" ,str);

 

}else{

NSLog(@"Failed to parse the XML");

}

}

 

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

 

 

// 文档开始

-(void)parserDidStartDocument:(NSXMLParser *)parser

{

self.rootElement = nil;

self.currentElementPointer = nil;

}

// 文档结束

-(void)parserDidEndDocument:(NSXMLParser *)parser

{

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;

NSLog(@"name:%@" , elementName);

}

// 元素结束

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

{

self.currentElementPointer = self.currentElementPointer.parent;

NSLog(@"end name:%@" , elementName);

}

// 解析文本,会多次解析,每次只解析1000个字符,如果多月1000个就会多次进入这个方法

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{

if([self.currentElementPointer.text length] > 0){

self.currentElementPointer.text = [self.currentElementPointer.textstringByAppendingString:string];

}else{

self.currentElementPointer.text = [NSMutableString stringWithString:string];

}

 

NSLog(@"string:%@" , string);

}

@end


你可能感兴趣的:(xml,XML文件解析)