Mainly on the file of three kinds of parsing methods, the relevant files have been compressed in the attachment
. h file
#import <UIKit/UIKit.h>
#import "StudentModel.h"
#import "GDataXMLNode.h"
@interface rootviewcontroller:uiviewcontroller<nsxmlparserdelegate>
@property (Nonatomic,retain) Nsmutablearray *dataarray;
Storing temporary variables
@property (nonatomic, retain) NSString *tempstr;
@end
. m file
#import "RootViewController.h"
@interface Rootviewcontroller ()
@end
@implementation Rootviewcontroller
-(void) Viewdidload {
[Super Viewdidload];
UIButton *button1 =[uibutton Buttonwithtype:uibuttontypecustom];
[Button1 settitle:@ "JSON" forstate:uicontrolstatenormal];
UIButton *button2 =[uibutton Buttonwithtype:uibuttontypecustom];
[Button2 settitle:@ "DOM" forstate:uicontrolstatenormal];
UIButton *button3 =[uibutton Buttonwithtype:uibuttontypecustom];
[Button3 settitle:@ "SAX" forstate:uicontrolstatenormal];
Button1.frame =cgrectmake (10, 80, 50, 20);
Button2.frame =cgrectmake (30,110, 50, 20);
Button3.frame =cgrectmake (80, 150, 50, 20);
Button1.layer.borderWidth = 1;
Button1.layer.borderColor =[uicolor Blackcolor]. Cgcolor;
Button2.layer.borderWidth = 1;
Button2.layer.borderColor =[uicolor Blackcolor]. Cgcolor;
Button3.layer.borderWidth = 1;
Button3.layer.borderColor =[uicolor Blackcolor]. Cgcolor;
[Self.view Addsubview:button1];
[Self.view Addsubview:button2];
[Self.view Addsubview:button3];
Self.view.backgroundColor =[uicolor Redcolor];
[Button1 addtarget:self Action: @selector (Change1) forcontrolevents:uicontroleventtouchupinside];
[Button2 addtarget:self Action: @selector (Change2) forcontrolevents:uicontroleventtouchupinside];
[Button3 addtarget:self Action: @selector (Change3) forcontrolevents:uicontroleventtouchupinside];
}
Parsing JSON
-(void) Change1
{
NSString *path=[[nsbundle mainbundle]pathforresource:@ "Student" oftype:@ "txt"];
Convert files to Nsjson based on path
NSData *data =[nsdata Datawithcontentsoffile:path];
Self.dataarray =[nsmutablearray Array];
Create error message
Nserror *error =nil;
Get the array (inside the dictionary)
Nsarray *array = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers error:& ERROR];
Iterating through an array
For (nsdictionary *dic in array) {
Studentmodel *stu =[studentmodel Getstudentmodel];
[Stu Setvaluesforkeyswithdictionary:dic];
[Self.dataarray Addobject:stu];
}
Results Print View
Studentmodel *stu1 =[self.dataarray objectatindex:1];
NSLog (@ "%@", stu1.name);
}
-(void) Change2
{
Dom parsing
NSString *path =[[nsbundle mainbundle]pathforresource:@ "Student" oftype:@ "xml"];
NSData *data =[nsdata Datawithcontentsoffile:path];
Self.dataarray =[nsmutablearray Array];
Reprint data (data is all added to memory, so DOM parsing consumes memory when parsing big data)
1. Parameter represents data 2 parameter is useless 3 parameter is error message
Nserror *error =nil;
Gdataxmldocument *document =[[gdataxmldocument alloc]initwithdata:data options:0 error:&error];
Get root node
Gdataxmlelement *rootelement =[document rootelement];
Get all child nodes
Nsarray *array =[rootelement elementsforname:@ "student"];
//
Traverse
For (gdataxmlelement *node in array) {
Studentmodel *stu =[studentmodel Getstudentmodel];
Stu.name = [[Node elementsforname:@ "name"]lastobject]stringvalue];
Stu.sex =[[[node elementsforname:@ "Sex"]lastobject]stringvalue];
Stu.number =[[[[node elementsforname:@ "number"]lastobject]stringvalue]integervalue];
Stu.phone =[[[[node elementsforname:@ "Phone"]lastobject]stringvalue]integervalue];
[Self.dataarray Addobject:stu];
//
// }
Studentmodel *model =[self.dataarray objectatindex:1];
NSLog (@ "%@", model.name);
NSString *path =[[nsbundle mainbundle]pathforresource:@ "Student" oftype:@ "xml"];
NSData *data =[nsdata Datawithcontentsoffile:path];
Self.dataarray =[nsmutablearray Array];
Nserror *error =nil;
Gdataxmldocument *document =[[gdataxmldocument alloc]initwithdata:data options:0 error:&error];
Gdataxmlelement *rootelement =[document rootelement];
Nsarray *array =[rootelement elementsforname:@ "student"];
For (gdataxmlelement *node in array) {
Studentmodel *stu =[studentmodel Getstudentmodel];
Stu.name =[[[node elementsforname:@ "name"]lastobject]stringvalue];
Stu.sex =[[[node elementsforname:@ "Sex"]lastobject]stringvalue];
Stu.number =[[[[node elementsforname:@ "number"]lastobject]stringvalue] integervalue];
Stu.phone = [[[[Node
elementsforname:@ "Phone"]lastobject]stringvalue]integervalue];
[Self.dataarray Addobject:stu];
}
Studentmodel *model =[self.dataarray objectatindex:1];
NSLog (@ "%@", model.name);
}
-(void) Change3
{
Parsing an XML file in sax mode
Get the file path to resolve
NSString *path =[[nsbundle mainbundle]pathforresource:@ "Student" oftype:@ "xml"];
Turn this file into a nsdata.
NSData *data =[nsdata Datawithcontentsoffile:path];
Creating a parsing class
Nsxmlparser *parser =[[nsxmlparser Alloc]initwithdata:data];
Set up Proxy
Parser.delegate =self;
Start parsing
[Parser parse];
}
-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string{
Discover node Content
Record node content
Self.tempstr =string;
}
#pragma-mark Sax Parsing proxy method
-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict
{
Head node
if ([elementname isequaltostring:@ "Students"]) {
Self.dataarray =[nsmutablearray Array];
} else if ([elementname isequaltostring:@ "Student"]) {
Studentmodel *stu =[studentmodel Getstudentmodel];
[Self.dataarray Addobject:stu];
}
}
-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) qname{
Discovery Tail Node
Remove Stdentmodel
Studentmodel *stu =[self.dataarray Lastobject];
if ([ElementName isequaltostring:@ "number"]) {
Stu.number =[self.tempstr IntegerValue];
} else if ([elementname isequaltostring:@ "name"]) {
Stu.name =self.tempstr;
}else if ([elementname isequaltostring:@ "Sex"]) {
Stu.sex =self.tempstr;
}else if ([elementname isequaltostring:@ "Phone"]) {
Stu.phone =[self.tempstr IntegerValue];
}
}
Parse complete
-(void) Parserdidenddocument: (Nsxmlparser *) parser{
NSLog (@ "Parse complete");
Studentmodel *stu =[self.dataarray objectatindex:1];
NSLog (@ "%@", stu.name);
}
@end
This article is from the iOS blog, so be sure to keep this source http://10136044.blog.51cto.com/10126044/1679371
IOS parsing JSON DOM SAX for a file