IOS ui13_data parsing XML _, JSON
-(IBAction) parserButton :( id) sender {
ParserXML * parser = [parserXML alloc] init];
[Parser startParser];
// Traverse the Student name
For (Student * stu in parser. stuArr ){
NSLog (@ % @, stu. name );
}
}
-(IBAction) domButton :( id) sender {
NSMutableArray * stuArr = [DOMXML DOMStart];
For (Student * stu in stuArr ){
NSLog (@ % @, stu. name );
}
}
-(IBAction) jsonButton :( id) sender {
// Parse local data
NSString * path = [[NSBundle mainBundle] pathForResource: @ movielist ofType: @ txt];
NSData * data = [NSData dataWithContentsOfFile: path];
// Specify a variable container to receive parsed data.
NSMutableDictionary * dic = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: nil];
NSMutableArray * movieArr = [NSMutableArray array];
For (NSMutableDictionary * temp in dic [@ result]) {
Movie * movie = [[Movie alloc] init];
[Movie setValuesForKeysWithDictionary: temp];
[MovieArr addObject: movie];
[Movie release];
}
}
ParserXML. h
# Import
# Import Student. h
@ Interface parserXML: NSObject
// Use an attribute and an array to hold all parsed model Objects
@ Property (nonatomic, retain) NSMutableArray * stuArr;
// String used to temporarily save node content
@ Property (nonatomic, retain) NSString * tempStr;
// Give a method for starting Parsing
-(Void) startParser;
@ End
ParserXML. m
//
// ParserXML. m
// UI13 _ data parsing XML _, JSON
//
// Created by dllo on 15/8/14.
// Copyright (c) 2015 zhozhicheng. All rights reserved.
//
# Import parserXML. h
@ Implementation parserXML
-(Void) startParser
{
// Obtain the file path based on the file name and type.
NSString * path = [[NSBundle mainBundle] pathForResource: @ Student ofType: @ xml];
// Convert the path file content into an NSData object and save it
NSData * data = [NSData dataWithContentsOfFile: path];
// Create an xml parsing Tool
NSXMLParser * parser = [[NSXMLParser alloc] initWithData: data];
// The agent for which the parsing tool needs to be set
Parser. delegate = self;
// Start Parsing
[Parser parse];
}
# How to Run pragma mark
-(Void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary *) attributeDict
{
// This method is automatically implemented when a node is hit.
If ([elementName isinclutostring: @ Student]) {
// When the root node Student is reached, the array needs to be initialized.
Self. stuArr = [NSMutableArray array];
} Else if ([elementName isinclutostring: @ student]) {
// When student is reached, it means that this is a new object. You need to create an object to receive data and put it in the student array.
Student * stu = [[Student alloc] init];
[Self. stuArr addObject: stu];
[Stu release];
}
}
-(Void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string
{
// You need to call this function when you locate the node content. you need to use an attribute to save the string of the corresponding node content.
// Save the node content
Self. tempStr = string;
}
-(Void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName
{
// This method will be used as long as the node ends
// The uncompleted object is located at the last position of the array.
Student * stu = [self. stuArr lastObject];
// If ([elementName isinclutostring: @ name]) {
// Stu. name = self. tempStr;
//}
// Assign values through KVC
[Stu setValue: self. tempStr forKey: elementName];
}
@ End