Here is the use of Nsxmlparser to parse, this is Apple's own XML parsing library, there is a reference article: http://www.raywenderlich.com/553/ Xml-tutorial-for-ios-how-to-choose-the-best-xml-parser-for-your-iphone-project
Add an XML file to Xcode:
<?xml version= "1.0" encoding= "iso-8859-1"? ><root><shop shoplocation= "Beijing" > <id> 001</id> <name>John</name> <url>Reminder</url> <info>don ' t Forget the Meeting!</info></shop><shop shoplocation= "Dongcheng" > <id>002</id> <name>Jack</name> <url>cc</url> <info>how is you!</info></ Shop><shop shoplocation= "Haidian" > <id>003</id> <name>Tom</name> <url>bb</url> <info>i am fine!</info></shop></root>
Read it first.
nsstring* path = [[nsbundle Mainbundle] pathforresource:@ "Shop" oftype:@ "xml"]; NSData *data = [[NSData alloc] initwithcontentsoffile:path options: (Nsdatareadingmappedifsafe) error:nil]; Nsxmlparser *parser=[[nsxmlparser alloc] initwithdata:tmpdata];<span style= "White-space:pre" ></span>[ Parser Setdelegate:self]; [Parser parse]; [parser release]; [Self parseshoplistfromresponse:data];
Then the parsing:
@property (Nonatomic,retain) shopdata* tmpshop; @property (nonatomic,retain) nsmutablestring* currentstring; @property (Nonatomic,retain) nsstring* Currenttag; #pragma mark Nsxmlparser delegate methods-(void) Parser: (Nsxmlparser *) Parser didstartelement: ( NSString *) elementname NamespaceURI: (NSString *) NamespaceURI qualifiedname: (NSString *) Qnameattributes: ( Nsdictionary *) attributedict {self.currenttag = elementname;if ([elementname isequaltostring:@ "root"]) {self.tmpList = [[Nsmutablearray alloc] init];} else if ([ElementName isequaltostring:@ "Shop"]) {self.tmpshop = [[Shopdata alloc] init]; <span style= "White-space:pre" ></span><span style= "font-family:arial, Helvetica, Sans-serif;" > </span><span class= "S1" style= "font-family:arial, Helvetica, Sans-serif;" >nsarray</span><span style= "font-family:arial, Helvetica, Sans-serif;" >* array = [attributedict </span><span class= "S2" style= "font-family:arial, Helvetica, Sans-serif;" ≫allkeys</span><span style= "font-family:arial, Helvetica, Sans-serif;" >];</span><p class= "P1" > <span style= "White-space:pre" ></span> <span class= "S1" >nsstring</span>* key = [array <span class= "S2" >lastobject</span>];</p ><p class= "P1" > <span style= "White-space:pre" ></span><span class= " S1 ">NSString</span>*s = [attributedict <span class=" S2 ">objectforkey</span>:key];</p ><p class= "P1" > <span style= "White-space:pre" ></span><span class= " S3 ">self</span>.<span class=" S4 ">tmpshop</span>.<span class=" S4 ">info</span> = S ;//Take to shoplocation this attribute </p>}}-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string {if ( Self.currentstring = = nil) {self.currentstring = [[Nsmutablestring alloc] initwithstring:@ ""]; }if ([Self.currenttag isequaltostring:@ "name"] | | [Self.currenttag isequaltostring:@ "id"] | | [Self.currenttag isequaltostring:@ "url"] | | [Self.currenttag isequaltostring:@ "info"]) {[self.currentstring appendstring:string];}} -(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName {if ([elementname isequaltostring:@ "Shop"]) {[Self.tmplist addObject:self.tmpShop]; [Self.tmpshop release];} if ([elementname isequaltostring:@ "name"]) {self.tmpShop.name = [self.currentstring copy]; [Self.currentstring setstring:@ ""];} else if ([elementname isequaltostring:@ "id"]) {self.tmpshop._id = [self.currentstring copy]; [Self.currentstring setstring:@ ""];} else if ([elementname isequaltostring:@ "url"]) {self.tmpShop.url = [self.currentstring copy]; [Self.currentstring setstring:@ ""];} else if ([elementname isequaltostring:@ "Info"]) {self.tmpShop.info = [self.currentstring copy]; [Self.currentstring setstring:@ ""];}} -(void) Parserdidstartdocument: (Nsxmlparser *) Parser {NSLog (@ "Start parsing xml file");} -(void) Parserdidenddocument: (Nsxmlparser *) parser {[Self.tableview reloaddata]; NSLog (@ "Parse XML file Complete");}
The structure of the Shopdata is:
@property (Nonatomic,retain) nsstring *name; @property (nonatomic,retain) nsstring *url; @property (Nonatomic,retain) NSString *_id; @property (nonatomic,retain) nsstring *info; @property (nonatomic, retain) UIImage *appicon;
The code can be downloaded in http://download.csdn.net/detail/baidu_nod/7585037 without the need for points