<span id="Label3"></p><p><p>XML file style style has a variety of styles, the standard is to put the data in the child node, there is a relatively simple reading is to put the data directly into the property inside</p></p>The first, this is a self-written one, the data stored in the child node, read relatively troublesome<br><br>The second is from the Chinese Meteorological Network API Shangjie District out of the paragraph, is to store the data in the attribute, it is easier to read the data<br><br>Here is the code to parse the first Style://header file<p><p>#import</p></p><p><p>@interface rootviewcontroller:uiviewcontroller<</span>nsxmlparserdelegate></p></p><p><p>#pragma mark-declaration--properties</p></p><p><p>@property (nonatomic, Retain) Nsmutablearray *messageelistarray; Where the data is stored</p></p><p><p>@property (nonatomic, retain) nsstring *fromtagflag; Start tag</p></p><p><p>@end</p></p><p><p></p></p>Implementation file<p><p>#import "RootViewController.h"</p></p><p><p>@interface Rootviewcontroller ()</p></p><p><p>@end</p></p><p><p>@implementation Rootviewcontroller</p></p><p><p>-(id) initwithnibname: (nsstring *) nibnameornil bundle: (nsbundle*) Nibbundleornil</p></p><p><p>{</p></p><p><p>Self = [super Initwithnibname:nibnameornil bundle:nibbundleornil];</p></p><p><p>If (self) {</p></p><p><p>Custom initialization</p></p><p><p>}</p></p><p><p>Return self;</p></p><p><p>}</p></p><p><p></p></p><p><p>-(void) Viewdidload</p></p><p><p>{</p></p><p><p>[super viewdidload];</p></p><p><p></p></p><p><p></p></p><p><p>Get XML String type file name</p></p><p><p>NSString *xmldocpath = [[nsbundle mainbundle] pathforresource:@ "document" oftype:@ "xml"];</p></p><p><p>Convert URL of String type to Nsurl type</p></p><p><p>Nsurl *url =[[nsurl alloc] initfileurlwithpath:xmldocpath];</p></p><p><p></p></p><p><p>Instantiating a network request instance using a URL</p></p><p><p>Nsurlrequest *request = [nsurlrequest requestwithurl:url];</p></p><p><p>Synchronizing data read to the network</p></p><p><p>NSData *urldata = [nsurlconnection SendSynchronousRequest:requestreturningResponse:nil error:nil];</p></p><p><p></p></p><p><p></p></p><p><p>Instantiate a specific parsing class object</p></p><p><p>Nsxmlparser *xmlparser = [[nsxmlparser alloc] initwithdata:urldata];</p></p><p><p></p></p><p><p>Set the proxy for this resolved class object</p></p><p><p>Xmlparser.delegate = self;</p></p><p><p>Invoking a method to begin parsing an XML file</p></p><p><p>[xmlparser parse];</p></p><p><p></p></p><p><p>}</p></p><p><p></p></p><p><p>#pragma mark-nsxmlparser Delegate-</p></p><p><p>#pragma mark Document starts reading</p></p><p><p>-(void) parserdidstartdocument: (nsxmlparser *) Parser {</p></p><p><p>Instantiate an array of stored data</p></p><p><p>Self.messageelistarray = [nsmutablearray array];</p></p><p><p>}</p></p><p><p>#pragma mark parsing tag starts</p></p><p><p>-(void) parser: (nsxmlparser *) parser didstartelement: (nsstring *) elementname namespaceuri: (nsstring *) NamespaceURI Qualifiedname: (nsstring *) qName attributes: (nsdictionary *) attributedict {</p></p><p><p>First get elementname with attribute Fromtagflag for later use</p></p><p><p>Self.fromtagflag = elementname;</p></p><p><p>Judge</p></p><p><p>If ([elementname isequaltostring:@ "Message"]) {</p></p><p><p>NSString *identifier = [attributedict objectforkey:@ "id"];</p></p><p><p>Nsmutabledictionary *onedic = [nsmutabledictionary dictionary];</p></p><p><p>[onedic Setobject:identifier forkey:@ "id"];</p></p><p><p>[self.messageelistarray addobject:onedic];</p></p><p><p>}</p></p><p><p>}</p></p><p><p>#pragma mark gets the data that corresponds to the label</p></p><p><p>-(void) parser: (nsxmlparser *) parser foundcharacters: (nsstring *) string {</p></p><p><p>Culling the obtained text hollow lattice and line wrapping</p></p><p><p>string = [string stringbytrimmingcharactersinset:[nscharactersetwhitespaceandnewlinecharacterset]];</p></p><p><p>Nsmutabledictionary *dict = [self.messageelistarray lastobject];</p></p><p><p></p></p><p><p>Judging from tags</p></p><p><p>If ([_fromtagflag isequaltostring:@ "from"] && Dict) {</p></p><p><p>[dict setobject:string forkey:@ "from"];</p></p><p><p>}</p></p><p><p>Judging CDate Tags</p></p><p><p>If ([_fromtagflag isequaltostring:@ "CDate"] && Dict) {</p></p><p><p>[dict setobject:string forkey:@ "CDate"];</p></p><p><p>}</p></p><p><p>Judging content Tags</p></p><p><p>If ([_fromtagflag isequaltostring:@ "Content"] && Dict) {</p></p><p><p>[dict setobject:string forkey:@ "Content"];</p></p><p><p>}</p></p><p><p>}</p></p><p><p>#pragma mark-end of parsing tag</p></p><p><p>-(void) parser: (nsxmlparser *) parser didendelement: (nsstring *) elementname namespaceuri: (nsstring *) NamespaceURI Qualifiedname: (nsstring *) QName {</p></p><p><p>Self.fromtagflag = nil;</p></p><p><p>}</p></p><p><p>#pragma mark document End read</p></p><p><p>-(void) parserdidenddocument: (nsxmlparser *) Parser {</p></p><p><p>To print an array of the obtained data</p></p><p><p>NSLog (@ "%@", self.messageelistarray);</p></p><p><p>}</p></p><p><p></p></p><p><p></p></p><p><p>#pragma Mark-override----dealloc method</p></p><p><p>-(void) Dealloc {</p></p><p><p>[_messageelistarray release], _messageelistarray = nil;</p></p><p><p>[_fromtagflag release], _fromtagflag = nil;</p></p><p><p></p></p><p><p>Call the Dealloc method of the parent class</p></p><p><p>[super dealloc];</p></p><p><p>}</p></p><p><p>@end</p></p>Code//header file for the second XML style parsing<p><p>#import</p></p><p><p>@interface rootviewcontroller:uiviewcontroller<</span>nsxmlparserdelegate></p></p><p><p>#pragma mark-declaration--properties</p></p><p><p>@property (nonatomic, Retain) Nsmutablearray *messageelistarray; Where the data is stored</p></p><p><p>@property (nonatomic, retain) nsstring *fromtagflag; Start tag</p></p><p><p>@end</p></p>Implementation file<p><p>#import "RootViewController.h"</p></p><p><p>@interface Rootviewcontroller ()</p></p><p><p>@end</p></p><p><p>@implementation Rootviewcontroller</p></p><p><p>-(id) initwithnibname: (nsstring *) nibnameornil bundle: (nsbundle*) Nibbundleornil</p></p><p><p>{</p></p><p><p>Self = [super Initwithnibname:nibnameornil bundle:nibbundleornil];</p></p><p><p>If (self) {</p></p><p><p>Custom initialization</p></p><p><p>}</p></p><p><p>Return self;</p></p><p><p>}</p></p><p><p></p></p><p><p>-(void) Viewdidload</p></p><p><p>{</p></p><p><p>[super viewdidload];</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Preparing string data for URLs</p></p><p><p>NSString *urlstring [email protected] "http://flash.weather.com.cn/wmaps/xml/beijing.xml";</p></p><p><p>Convert URL of String type to Nsurl type</p></p><p><p>Nsurl *URL1 = [[nsurl alloc] initwithstring:urlstring];</p></p><p><p>Instantiating a network request instance using a URL</p></p><p><p>Nsurlrequest *request = [nsurlrequest requestwithurl:url1];</p></p><p><p>Synchronizing data read to the network</p></p><p><p>NSData *urldata = [nsurlconnection SendSynchronousRequest:requestreturningResponse:nil error:nil];</p></p><p><p></p></p><p><p></p></p><p><p>Instantiate a specific parsing class object</p></p><p><p>Nsxmlparser *xmlparser = [[nsxmlparser alloc] initwithdata:urldata];</p></p><p><p></p></p><p><p>Set the proxy for this resolved class object</p></p><p><p>Xmlparser.delegate = self;</p></p><p><p>Invoking a method to begin parsing an XML file</p></p><p><p>[xmlparser parse];</p></p><p><p></p></p><p><p>}</p></p><p><p></p></p><p><p>#pragma mark-nsxmlparser Delegate-</p></p><p><p>#pragma mark Document starts reading</p></p><p><p>-(void) parserdidstartdocument: (nsxmlparser *) Parser {</p></p><p><p>Instantiate an array of stored data</p></p><p><p>Self.messageelistarray = [nsmutablearray array];</p></p><p><p>}</p></p><p><p>#pragma mark parsing tag starts</p></p><p><p>-(void) parser: (nsxmlparser *) parser didstartelement: (nsstring *) elementname namespaceuri: (nsstring *) NamespaceURI Qualifiedname: (nsstring *) qName attributes: (nsdictionary *) attributedict {</p></p><p><p>First get elementname with attribute Fromtagflag for later use</p></p><p><p>Self.fromtagflag = elementname;</p></p><p><p><span style="font-family: ‘Heiti SC Light‘;">since the data is stored in the attribute, it is all stored in the attributedict, where it is printed directly under</span> the</p></p><p><p>NSLog (@ "%@", attributedict);</p></p><p><p>}</p></p><p><p>#pragma mark gets the data that corresponds to the label</p></p><p><p>-(void) parser: (nsxmlparser *) parser foundcharacters: (nsstring *) string {</p></p><p><p>Because the data is stored in the attribute, No child tags need to be parsed</p></p><p><p>}</p></p><p><p>#pragma mark-end of parsing tag</p></p><p><p>-(void) parser: (nsxmlparser *) parser didendelement: (nsstring *) elementname namespaceuri: (nsstring *) NamespaceURI Qualifiedname: (nsstring *) QName {</p></p><p><p>Self.fromtagflag = nil;</p></p><p><p>}</p></p><p><p>#pragma mark document End read</p></p><p><p>-(void) parserdidenddocument: (nsxmlparser *) Parser {</p></p><p><p>To print an array of the obtained data</p></p><p><p>NSLog (@ "%@", self.messageelistarray);</p></p><p><p>}</p></p><p><p></p></p><p><p></p></p><p><p>#pragma Mark-override----dealloc method</p></p><p><p>-(void) Dealloc {</p></p><p><p>[_messageelistarray release], _messageelistarray = nil;</p></p><p><p>[_fromtagflag release], _fromtagflag = nil;</p></p><p><p></p></p><p><p>Call the Dealloc method of the parent class</p></p><p><p>[super dealloc];</p></p><p><p>}</p></p><p><p></p></p><p><p>@end</p></p><p><p>XML parsing for iOS</p></p></span>
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