码迷,mamicode.com
首页 > 移动开发 > 详细

IOS解析XML文件

时间:2014-07-03 15:31:49      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:ios xml解析

这里使用NSXMLParser来解析,这个是apple自带的xml解析库,有个参考文章:http://www.raywenderlich.com/553/xml-tutorial-for-ios-how-to-choose-the-best-xml-parser-for-your-iphone-project

在Xcode中加入一个xml文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<Shop shoplocation="beijing">
    <id>001</id>
    <name>John</name>
    <url>Reminder</url>
    <info>Don't forget the meeting!</info>
</Shop>
<Shop shoplocation="dongcheng">
    <id>002</id>
    <name>Jack</name>
    <url>cc</url>
    <info>how are you!</info>
</Shop>
<Shop shoplocation="haidian">
    <id>003</id>
    <name>Tom</name>
    <url>bb</url>
    <info>I am fine!</info>
</Shop>
</root>



先读出来

   NSString* path =  [[NSBundle mainBundle] pathForResource:@"shop" ofType:@"xml"];
    NSData *data = [[NSData alloc] initWithContentsOfFile:path options:(NSDataReadingMappedIfSafe) error:nil];
    NSXMLParser *parser=[[NSXMLParser alloc] initWithData:tmpdata];
<span style="white-space:pre">	</span>[parser setDelegate:self];
	[parser parse];
	[parser release];
    [self parseShopListFromResponse:data];


然后进行解析:

@property(nonatomic,retain)ShopData* tmpShop;
@property(nonatomic,retain)NSMutableString* currentString;
@property(nonatomic,retain)NSString* currentTag;
#pragma mark NSXMLParser delegate methods
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
	attributes: (NSDictionary *)attributeDict {
    self.currentTag = elementName;
	if ([elementName isEqualToString:@"root"]) {
		self.tmpList = [[NSMutableArray alloc] init];
	}else if ([elementName isEqualToString:@"Shop"]) {
		self.tmpShop = [[ShopData alloc] init];
        <span style="white-space:pre">	</span><span style="font-family: Arial, Helvetica, sans-serif;"> </span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;">NSArray</span><span style="font-family: Arial, Helvetica, sans-serif;">* array = [attributeDict </span><span class="s2" style="font-family: Arial, Helvetica, sans-serif;">allKeys</span><span style="font-family: Arial, Helvetica, sans-serif;">];</span><p class="p1">        <span style="white-space:pre">	</span><span class="s1">NSString</span>* key = [array <span class="s2">lastObject</span>];</p><p class="p1">        <span style="white-space:pre">	</span><span class="s1">NSString</span>*s  = [attributeDict <span class="s2">objectForKey</span>:key];</p><p class="p1">        <span style="white-space:pre">	</span><span class="s3">self</span>.<span class="s4">tmpShop</span>.<span class="s4">info</span> = s;//取到ShopLocation这个属性</p>
	}
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (self.currentString == nil) {
        self.currentString = [[NSMutableString alloc] initWithString:@""];
    }
    
    if ([self.currentTag isEqualToString:@"name"] ||
        [self.currentTag isEqualToString:@"id"] ||
        [self.currentTag isEqualToString:@"url"] ||
        [self.currentTag isEqualToString:@"info"]) {
        [self.currentString appendString:string];
	}
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
 
	if ([elementName isEqualToString:@"Shop"]) {
		[self.tmpList addObject:self.tmpShop];
        [self.tmpShop release];
	} if ([elementName isEqualToString:@"name"]) {
        self.tmpShop.name = [self.currentString copy];
        [self.currentString setString:@""];
	}else if ([elementName isEqualToString:@"id"]) {
		self.tmpShop._id = [self.currentString copy];
        [self.currentString setString:@""];
	}else if ([elementName isEqualToString:@"url"]) {
		self.tmpShop.url = [self.currentString copy];
        [self.currentString setString:@""];
	}else if ([elementName isEqualToString:@"info"]) {
		self.tmpShop.info = [self.currentString copy];
        [self.currentString setString:@""];
	}
}

- (void)parserDidStartDocument:(NSXMLParser *)parser {
	NSLog(@"开始解析xml文件");
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
	
	[self.tableView reloadData];
	NSLog(@"解析xml文件完成");
}


Shopdata的结构是:

@property (nonatomic,retain) NSString *name;
@property (nonatomic,retain) NSString *url;
@property (nonatomic,retain) NSString *_id;
@property (nonatomic,retain) NSString *info;
@property (nonatomic, retain) UIImage *appIcon;



代码可以在http://download.csdn.net/detail/baidu_nod/7585037下载,无需积分

IOS解析XML文件,布布扣,bubuko.com

IOS解析XML文件

标签:ios xml解析

原文地址:http://blog.csdn.net/baidu_nod/article/details/36515883

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!