iOS parsing XML file

Source: Internet
Author: User

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;" &Gt;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

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.