Iphonexml Parsing Method

Source: Internet
Author: User
Tags call back

There should be many XML parsing methods for iPhone, which can call the xmlib library or something, which is more convenient. Here we only post a clumsy method that uses proxy callback to asynchronously Parse XML

/********** Declare a proxy (only one header file exists in the file where you call to parse the XML class ), define the object in the resolution Class header file. After the resolution is complete, call back the function of the same name on the page and update the page **********/

/******************** <Br/> * @ resolve the list of accounts bound to the user imsi <br/> * callback Function protocol declaration <br/> *********************/<br/> # import <uikit/uikit. h> <br/> @ protocol parsebindaccountdelegate <br/> // The callback function executed after the message is parsed. The callback function becomes a protocol and needs the same name on the page. <br/>-(void) parsebindaccountcallback :( nsmutablearray *) myallaccountdatas; <br/> @ end

/*********** Resolve Class header files ****************/

/****************** <Br/> * @ resolution class for binding imsi to all account lists <br/> * parse all accounts, reserve IDs for user name switching <br/> *************************/<br/> # import <Foundation/Foundation. h> <br/> # import "bindaccountdata. H "<br/> # import" parsebindaccountdelegate. H "<br/> # import" util. H "<br/> @ interface parsebindaccount: nsobject {<br/> id <parsebindaccountdelegate> * callback; // The callback function declared by the proxy protocol <br/> nsmutablearray * mybindaccountdatas; // a collection of all account information <br/> nsmutabledata * connectdata; // transit data of the general request URL; <br/> bindaccountdata * onebindaccountdata; // one bound Account Information <br/>}< br/> @ property (nonatomic, retain) ID <parsebindaccountdelegate> * myparsebindaccountdelegate; // proxy protocol, declare the callback function <br/> @ property (nonatomic, retain) nsmutablearray * mybindaccountdatas; // a collection of all account information <br/> @ property (nonatomic, retain) nsmutabledata * connectdata; // transit data of the general request URL; <br/> @ property (nonatomic, retain) bindaccountdata * onebindaccountdata; // one bound Account Information <br/>/* <br/> call the parsing method to parse XML <br/> */<br/>-(void) parsebindaccount :( nsmutabledata *) indata; </P> <p> @ end

***************/

/****************** <Br/> * @ resolution class for binding imsi to all account lists <br/> * parse all accounts, reserve IDs for user name switching <br/> *************************/<br/> # import "parsebindaccount. H "<br/> @ implementation parsebindaccount <br/> @ synthesize myparsebindaccountdelegate; // declare the callback function using the proxy protocol <br/> @ synthesize mybindaccountdatas; // a collection of all account information <br/> @ synthesize connectdata; // transit data of a general request URL; <br/> @ synthesize onebindaccountdata; // one bound Account Information <br/>/* <br/> call resolution Method parsing XML <br/> */<br/>-(void) parsebindaccount :( nsmutabledata *) indata {<br/> [util showlog: @ "parsebindaccount"]; <br/> // to init data <br/> mybindaccountdatas = [[nsmutablearray alloc] init]; // All sys MSG in it <br/> onebindaccountdata = [[bindaccountdata alloc] init]; <br/> connectdata = [[nsmutabledata alloc] init]; // XML data allocation space <br/> connectdata = indata; // recive data of XML </P> <p> nsxmlparser * paser = [NSX Mlparser alloc] initwithdata: connectdata]; <br/> [paser setdelegate: Self]; <br/> [paser parse]; // execute this sentence to start the callback parsing function-parserdidstartdocument-didstartelement <br/> [paser release]; <br/> [connectdata release]; <br/>}< br/>/* <br/> Start parsing the XML file <br/> */<br/>-(void) parserdidstartdocument :( nsxmlparser *) parser {<br/> [util showlog: @ "parserdidstartdocument"]; <br/>}< br/>/* <br/> Start parsing an XML Element and initialize the element. <B R/> */<br/>-(void) parser :( nsxmlparser *) parser didstartelement :( nsstring *) elementname namespaceuri :( nsstring *) namespaceuri qualifiedname :( nsstring *) qualifiedname attributes :( nsdictionary *) attributedict {<br/> [util showlog: @ "didstartelement"]; <br/> [onebindaccountdata release]; <br/> onebindaccountdata = [[bindaccountdata alloc] init]; <br/> If ([elementname is1_tostring: @ "list"]) {<br/> [util Showlog: @ "Enter list"]; <br/> nsstring * Action = [attributedict objectforkey: @ "action"]; <br/> onebindaccountdata. action = action; <br/>} else if ([elementname isinclutostring: @ "item"]) {<br/> // an item may fail, however, some operations may also require a successful message to determine whether the action is an error or msg. <br/> If ([onebindaccountdata. action compare: @ "true"] = 0) {<br/> onebindaccountdata. userid = [attributedict objectforkey: @ "userid"]; <br/> onebindaccountdata. loginnam E = [attributedict objectforkey: @ "loginname"]; <br/> onebindaccountdata. mainflag = [attributedict objectforkey: @ "mainflag"]; </P> <p >}else {// error <br/> onebindaccountdata. error = [attributedict objectforkey: @ "error"]; <br/>}< br/> [mybindaccountdatas addobject: onebindaccountdata]; </P> <p >}< br/>/* <br/> Add the obtained characters to the end of the element. <br/> * /<br/>-(void) parser :( nsxmlparser *) parser foundcharacters :( nsstring *) String {<br/> [util showlog: @ "foundcharacters"]; <br/>}< br/>/* <br/> after parsing the XML element, add the element to the array elements. <br/> */<br/> -( void) parser :( nsxmlparser *) paser didendelement :( nsstring *) elementname namespaceuri :( nsstring *) namespaceuri qualifiedname :( nsstring *) QNAME {<br/> [util showlog: @ "didendelement"]; <br/>}< br/>/* <br/> after the XML parsing is completed, the relevant information is displayed on the interface. <br/> */<br/>-(void) parserdidenddocument :( nsxmlparser *) Parser {<br/> [util showlog: @ "parserdidenddocument"]; <br/> If (self. myparsebindaccountdelegate! = Nil) {<br/> // The callback function is called by the completion thread. <br/> [self. myparsebindaccountdelegate parsebindaccountcallback: mybindaccountdatas]; <br/>}< br/> [myparsebindaccountdelegate release]; <br/>}< br/> @ end

/************* XML file ****************/

<? XML version = "1.0" encoding = "UTF-8"?> <List action = "true"> <item userid = "11111111111111" loginname = "safasdfas" mainflag = "0"/> <item userid = "2222222222222222222" loginname = "sadfasd" mainflag = "0"/> <item userid = "3333333333333333" loginname = "adfsd" mainflag = "0"/> <item userid = "455555555555" loginname = "dsafdsaf" mainflag =" 0 "/> <item userid =" 44444444444444 "loginname = dsfdsf" mainflag = "1"/> </List>"

 

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.