Ios json/XML Parsing
/* ------------------------- XML_SAX parse handler * // xml sax resolution-(IBAction) XML_SAX :( id) sender {NSData * da = [[NSData alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "megage" ofType: @ "txt"]; // Step 1: Create an xml parsing NSXMLParser * Parserdata = [[NSXMLParser alloc] initWithData: da]; // Step 2 sets the proxy Parserdata. delegate = self; // step 3 starts parsing [Parserdata parse]; NSLog (@ "");} /* ------------------------- XML_SAX proxy method restart * // start parsing -------------> Start parsing by using the proxy method-(void) parserDidStartDocument :( NSXMLParser *) parser {/1. the array of received information is initialized at the beginning of parsing, in order not to occupy the memory self. megArray = [NSMutableArray array];} // when a node is parsed-(void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary *) attributeDict {// small 1. receive his node self. elementString = elementName; if ([elementName isinclutostring: @ "message"]) {Recorce * meg = [[Recorce alloc] init]; [self. megArray addObject: meg] ;}// locate content (node)-(void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string {Recorce * meg = [self. megArray lastObject]; // Add content through the node [meg setValue: string forKey: self. elementString];} // end parsing a node-(void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName {self. elementString = nil;} // complete resolution-(void) parserDidEndDocument :( NSXMLParser *) parser {for (Recorce * me in self. megArray) {NSLog (@ "% @", me) ;}// when an error occurs in parsing-(void) parser :( NSXMLParser *) parser parseerroccurred :( NSError *) parseError {}
-(IBAction) XML_DOM :( id) sender {NSData * data = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "megage" ofType: @ "txt"]; // obtain the data GDataXMLDocument * document = [[GDataXMLDocument alloc] initWithData: data options: 0 error: nil]; // locate the root node GDataXMLElement * Rootelement = document. rootElement; self. megArray = [NSMutableArray array]; // traverses sub-nodes in the root node, for (GDataXMLElement * em in Rootelement. children) {message * meg = [[message alloc] init]; for (GDataXMLElement * GE in em. children) {[meg setValue: GE. stringValue forKey: GE. name];} [self. megArray addObject: meg]; NSLog (@ "% @", meg );}}
-(IBAction) JSON_KIT :( id) sender {// obtain the path NSData * data = [[NSData alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "content" ofType: @ "txt"]; // obtain data (third-party) NSArray * dataArray = [data objectFromJSONData]; NSLog (@ "% @", dataArray );}
- (IBAction)JSON:(id)sender{ NSData *data = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"content" ofType:@"txt"]]; NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];