Ios json/XML Parsing

Source: Internet
Author: User

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];


Related Article

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.