Shimen the main contribution, respect the work of the author, please do not reprint.
If the article is helpful to you, you are welcome to donate to the author, support the Shimen, donate the amount at random, ^_^
I want to donate: Click Donate
Cocos2d-x source Download: Dot I teleport
Games Official Download:HTTP://DWZ.CN/RWTJL
Game video preview:http://dwz.cn/RzHHd
Game Development Blog:Http://dwz.cn/RzJzI
Game Source Transfer : Http://dwz.cn/Nret1
In communication with the iphone and back-office systems, using Web service to get the most common form of server data.
In Web service communication, the data Interchange format usually uses XML data, and XML data is also a universal format in software development.
Therefore, XML parsing is also a basic skill that must be mastered in project development.
This article provides an XML data parsing class Simplexmlparser,
The characteristics of this class will parse the XML data into a database record form, and the data is parsed and saved in Nsarray.
where each element of an array corresponds to a database record, each record is a nsdictionary that consists 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 record is saved in the Recordstack variable of the parser object, first getting the number of record bars, and then traversing each record.
Nsinteger i = 0, nresult = [Thisparser getrecordcount];for (i = 0; i < Nresult; i++) {nsdictionary *recorddict = [ThisPar Ser getrecordatindex:i]; Nsdictionary *tempdic = [recorddict objectforkey:@ "Tempdic"]; NSString *value = [Tempdic objectforkey:@ "value"];}
Simplexmlparser class : click I download
If no points, the blog below reply to leave an email address.
Shimen the main contribution, respect the work of the author, please do not reprint.
If the article is helpful to you, you are welcome to donate to the author, support the Shimen, donate the amount at random, ^_^
I want to donate: Click Donate
Cocos2d-x source Download: Dot I teleport
Games Official Download:HTTP://DWZ.CN/RWTJL
Game video preview:http://dwz.cn/RzHHd
Game Development Blog:Http://dwz.cn/RzJzI
Game Source Transfer : Http://dwz.cn/Nret1
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Web service and XML data parsing for iOS Development Network chapter