你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS解析XML文件

IOS解析XML文件

編輯:IOS開發綜合

 

在Xcode中加入一個xml文件:

 




    001
    John
    Reminder
    Don't forget the meeting!


    002
    Jack
    cc
    how are you!


    003
    Tom
    bb
    I am fine!



 


先讀出來

 

 

   NSString* path =  [[NSBundle mainBundle] pathForResource:@shop ofType:@xml];
    NSData *data = [[NSData alloc] initWithContentsOfFile:path options:(NSDataReadingMappedIfSafe) error:nil];
    NSXMLParser *parser=[[NSXMLParser alloc] initWithData:tmpdata];
	[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];
        	 NSArray* array = [attributeDict allKeys];

        NSString* key = [array lastObject];

        NSString*s  = [attributeDict objectForKey:key];

        self.tmpShop.info = s;//取到ShopLocation這個屬性

} } -(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下載,無需積分

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved