Web Service and XML data parsing in iOS Development Network
Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.
If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^
I want to donate: Click to donate
Cocos2d-X source code download: point I send
Game official download: http://dwz.cn/RwTjl
Game video preview: http://dwz.cn/RzHHd
Game Development blog: http://dwz.cn/RzJzI
Game source code transfer: Http://dwz.cn/Nret1
In the communication between the iPhone and the background system, the most common form of obtaining server data using Web Service.
In Web Service communication, XML data is usually used in data exchange formats. XML data is also a common format in software development,
Therefore, XML parsing becomes a basic skill in project development.
This article provides an XML data parsing class SimpleXMLParser,
This type of feature parses XML data into a database record, and the data is parsed and saved in NSArray,
Each element of the array corresponds to a database record, and each record is a NSDictionary consisting of database fields and values.
1. Interface Definition
@interface SimpleXMLParser : NSObject{NSString *currentPropertyName;NSMutableString *currentData;BOOL contentError;NSString *faultName;NSString *faultName2;NSMutableData *webData;NSMutableArray *theMainStack;NSMutableArray *currentDataStack;NSXMLParser *parser;NSString *recordHead;NSInteger sectionCount;BOOL beginRecord;NSMutableString *currentCharacters;NSMutableDictionary *parsedResponseDictionary;NSString *parseElementPath;}- (id)init;- (void)initData:(NSMutableData *)data;- (void)parse:(NSString*)head;- (BOOL)isContentFault;- (NSInteger)getRecordCount;- (NSDictionary*)getRecordAtIndex:(NSInteger)idx;- (NSMutableArray*)getRecordLists;
2. Use
NSMutableData *webData;SimpleXMLParser *thisParser = [[SimpleXMLParser alloc] initWithData:webData];NSString *recordHead = @"User";[thisParser parse:recordHead];
The parsed database records are saved in the recordStack variable of the Parser object. The number of records is obtained first, and each record is traversed.
NSInteger i = 0, nResult = [thisParser getRecordCount];for(i = 0; i < nResult; i++){NSDictionary *recordDict = [thisParser getRecordAtIndex:i];NSDictionary *tempDic = [recordDict objectForKey:@"tempDic"];NSString *value = [tempDic objectForKey:@"value"];}
SimpleXMLParser class: Click to download
If you do not have any credits, you can leave an Email address in the reply below.
Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.
If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^
I want to donate: Click to donate
Cocos2d-X source code download: point I send
Game official download: http://dwz.cn/RwTjl
Game video preview: http://dwz.cn/RzHHd
Game Development blog: http://dwz.cn/RzJzI
Game source code transfer: Http://dwz.cn/Nret1
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.