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 and support the Shimen master. The donation amount is arbitrary, heavy in the mind ^_^
I want to donate: Click Donate
Cocos2d-x source code 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 code transfer : Http://dwz.cn/Nret1
Use Web service to get the most common form of server data in communication between iphone and back-office systems.
In the Web service communication. Data interchange formats typically use XML data, and XML data is a common form of 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.
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 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 following 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 any amount, ^_^
I want to donate: Click Donate
Cocos2d-x source code 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 code transfer : Http://dwz.cn/Nret1
Parsing Web service and XML data for the iOS Development Network article