Parsing of IOS XML data

Source: Internet
Author: User

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

The first, this is a self-written one, the data stored in the child node, read relatively troublesome

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

Here is the code to parse the first style://header file

#import

@interface rootviewcontroller:uiviewcontroller<</span>nsxmlparserdelegate>

#pragma mark-declaration--Properties

@property (nonatomic, retain) Nsmutablearray *messageelistarray; Where the data is stored

@property (nonatomic, retain) NSString *fromtagflag; Start tag

@end

Implementation file

#import "RootViewController.h"

@interface Rootviewcontroller ()

@end

@implementation Rootviewcontroller

-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (nsbundle*) Nibbundleornil

{

self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];

if (self) {

Custom initialization

}

return self;

}

-(void) viewdidload

{

[Super Viewdidload];

Get XML String type file name

NSString *xmldocpath = [[NSBundle mainbundle] pathforresource:@ "Document" oftype:@ "xml"];

Convert URL of String type to Nsurl type

Nsurl *url =[[nsurl alloc] Initfileurlwithpath:xmldocpath];

Instantiating a network request instance using a URL

Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];

Synchronizing data read to the network

NSData *urldata = [nsurlconnection sendSynchronousRequest:requestreturningResponse:nil error:nil];

Instantiate a specific parsing class object

Nsxmlparser *xmlparser = [[Nsxmlparser alloc] initwithdata:urldata];

Set the proxy for this resolved class object

Xmlparser.delegate = self;

Invoking a method to begin parsing an XML file

[Xmlparser parse];

}

#pragma mark-nsxmlparser Delegate-

#pragma mark Document starts reading

-(void) Parserdidstartdocument: (Nsxmlparser *) Parser {

Instantiate an array of stored data

Self.messageelistarray = [Nsmutablearray array];

}

#pragma mark parsing tag starts

-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict {

First get elementname with attribute Fromtagflag for later use

Self.fromtagflag = elementname;

Judge

if ([ElementName isequaltostring:@ "Message"]) {

NSString *identifier = [attributedict objectforkey:@ "id"];

Nsmutabledictionary *onedic = [Nsmutabledictionary dictionary];

[Onedic setobject:identifier forkey:@ "id"];

[Self.messageelistarray Addobject:onedic];

}

}

#pragma mark gets the data that corresponds to the label

-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string {

Culling the obtained text hollow lattice and line wrapping

string = [string Stringbytrimmingcharactersinset:[nscharactersetwhitespaceandnewlinecharacterset]];

Nsmutabledictionary *dict = [Self.messageelistarray lastobject];

Judging from tags

if ([_fromtagflag isequaltostring:@ "from"] && dict) {

[Dict setobject:string forkey:@ "from"];

}

Judging CDate Tags

if ([_fromtagflag isequaltostring:@ "CDate"] && dict) {

[Dict setobject:string forkey:@ "CDate"];

}

Judging content Tags

if ([_fromtagflag isequaltostring:@ "Content"] && dict) {

[Dict setobject:string forkey:@ "Content"];

}

}

#pragma mark-end of parsing tag

-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName {

Self.fromtagflag = nil;

}

#pragma mark document End read

-(void) Parserdidenddocument: (Nsxmlparser *) Parser {

To print an array of the obtained data

NSLog (@ "%@", Self.messageelistarray);

}

#pragma mark-Override----Dealloc method

-(void) Dealloc {

[_messageelistarray release], _messageelistarray = nil;

[_fromtagflag release], _fromtagflag = nil;

Call the Dealloc method of the parent class

[Super Dealloc];

}

@end

Code//header file for the second XML style parsing

#import

@interface rootviewcontroller:uiviewcontroller<</span>nsxmlparserdelegate>

#pragma mark-declaration--Properties

@property (nonatomic, retain) Nsmutablearray *messageelistarray; Where the data is stored

@property (nonatomic, retain) NSString *fromtagflag; Start tag

@end

Implementation file

#import "RootViewController.h"

@interface Rootviewcontroller ()

@end

@implementation Rootviewcontroller

-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (nsbundle*) Nibbundleornil

{

self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];

if (self) {

Custom initialization

}

return self;

}

-(void) viewdidload

{

[Super Viewdidload];

Preparing string data for URLs

NSString *urlstring [email protected] "http://flash.weather.com.cn/wmaps/xml/beijing.xml";

Convert URL of String type to Nsurl type

Nsurl *URL1 = [[Nsurl alloc] initwithstring:urlstring];

Instantiating a network request instance using a URL

Nsurlrequest *request = [Nsurlrequest REQUESTWITHURL:URL1];

Synchronizing data read to the network

NSData *urldata = [nsurlconnection sendSynchronousRequest:requestreturningResponse:nil error:nil];

Instantiate a specific parsing class object

Nsxmlparser *xmlparser = [[Nsxmlparser alloc] initwithdata:urldata];

Set the proxy for this resolved class object

Xmlparser.delegate = self;

Invoking a method to begin parsing an XML file

[Xmlparser parse];

}

#pragma mark-nsxmlparser Delegate-

#pragma mark Document starts reading

-(void) Parserdidstartdocument: (Nsxmlparser *) Parser {

Instantiate an array of stored data

Self.messageelistarray = [Nsmutablearray array];

}

#pragma mark parsing tag starts

-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict {

First get elementname with attribute Fromtagflag for later use

Self.fromtagflag = elementname;

since the data is stored in the attribute, it is all stored in the attributedict, where it is printed directly under the

NSLog (@ "%@", attributedict);

}

#pragma mark gets the data that corresponds to the label

-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string {

Because the data is stored in the attribute, no child tags need to be parsed

}

#pragma mark-end of parsing tag

-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName {

Self.fromtagflag = nil;

}

#pragma mark document End read

-(void) Parserdidenddocument: (Nsxmlparser *) Parser {

To print an array of the obtained data

NSLog (@ "%@", Self.messageelistarray);

}

#pragma mark-Override----Dealloc method

-(void) Dealloc {

[_messageelistarray release], _messageelistarray = nil;

[_fromtagflag release], _fromtagflag = nil;

Call the Dealloc method of the parent class

[Super Dealloc];

}

@end

Parsing of IOS XML data

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.