Web service and XML data parsing for iOS Development Network chapter

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.