Iphone parsing xml & json-1

Source: Internet
Author: User
Tags xml parser

XML file parsing example.
Code 1 and Code 2 are the basic content of IOS development.
Code 1.
# Import <UIKit/UIKit. h>
 
@ Class XmlTestViewController;
 
@ Interface XmlTestAppDelegate: NSObject <UIApplicationDelegate> {
UIWindow * window;
XmlTestViewController * viewController;
}
@ Property (nonatomic, retain) IBOutlet UIWindow * window;
@ Property (nonatomic, retain) IBOutlet XmlTestViewController * viewController;
 
@ End
 
Code 2
1 # import "XmlTestAppDelegate. h"
2 # import "XmlTestViewController. h"
3 @ implementation XmlTestAppDelegate
4 @ synthesize window;
5 @ synthesize viewController;
6 # pragma mark-
7 # pragma mark Application lifecycle
8-(BOOL) application :( UIApplication *) application
9 didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {
10 [window addSubview: viewController. view];
11 [window makeKeyAndVisible];
12 return YES;
13}
14-(void) applicationWillResignActive :( UIApplication *) application {}
15-(void) applicationDidEnterBackground :( UIApplication *) application {}
16-(void) applicationWillEnterForeground :( UIApplication *) application {}
17-(void) applicationDidBecomeActive :( UIApplication *) application {}
18-(void) applicationWillTerminate :( UIApplication *) application {}
19-(void) applicationDidReceiveMemoryWarning :( UIApplication *) application {}
20-(void) dealloc {
21 [viewController release];
22 [window release];
23 [super dealloc];
24}
25 @ end

<BR>
Code 3 is the controller header file.
Code 3

# Import <UIKit/UIKit. h>
 
@ Interface XmlTestViewController: UIViewController {
}
@ End
Controller implementation file. A string of the NSString type is declared here. Use the classes defined in code 5 and code 6 to parse them.
Code 4
# Import "XmlTestViewController. h"
# Import "FirstXmlParse. h"
@ Implementation XmlTestViewController

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(Void) viewDidLoad {
[Super viewDidLoad];
NSString * xml = @ "<xml>"
"<Title> title </title>"
"<Content> content description </content>"
"<Time> 2011-1-1 </time>"
"</Xml> ";

FirstXmlParse * first = [[FirstXmlParse alloc] init];
[First startParse: xml];
NSLog (@ "title: % @", first. title );
NSLog (@ "content: % @", first. content );
NSLog (@ "time: % @", first. time );
}

/*
// Override to allow orientations other than the default portrait orientation.
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation {
// Return YES for supported orientations
Return (interfaceOrientation = UIInterfaceOrientationPortrait );
}
*/

-(Void) didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[Super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

-(Void) viewDidUnload {
// Release any retained subviews of the main view.
// E.g. self. myOutlet = nil;
}

-(Void) dealloc {
[Super dealloc];
}
@ End

 
Code 5

# Import <Foundation/Foundation. h>
 
@ Interface FirstXmlParse: NSObject <NSXMLParserDelegate> {
NSString * title;
NSString * content;
NSString * time;
// Define a variable string
NSMutableString * contentString;
}
@ Property (nonatomic, retain) NSString * title;
@ Property (nonatomic, retain) NSString * content;
@ Property (nonatomic, retain) NSString * time;
@ Property (nonatomic, retain) NSMutableString * contentString;
-(Void) startParse :( NSString *) xml;
@ End
Code 6. This is the key to parsing xml files.
# Import "FirstXmlParse. h"

@ Implementation FirstXmlParse
@ Synthesize title;
@ Synthesize content;
@ Synthesize time;
@ Synthesize contentString;

-(Void) startParse :( NSString *) xml {
NSData * data = [xml dataUsingEncoding: NSUTF8StringEncoding];
// Create an xml Parser
NSXMLParser * xmlParse = [[NSXMLParser alloc] initWithData: data];
// Set Delegation
[XmlParse setDelegate: self];
[XmlParse parse];
}

// Starts to parse the document
-(Void) parserDidStartDocument :( NSXMLParser *) parser {
NSLog (@ "Resolution document start ");
}

-(Void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary *) attributeDict {
NSLog (@ "start tag: % @", elementName );
Self. contentString = [NSMutableString string];
}

-(Void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string {
NSLog (@ "encountered content: % @", string );
[Self. contentString appendString: string];
}

-(Void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName {
NSLog (@ "end tag: % @", elementName );
If ([elementName isEqualToString: @ "title"]) {
Title = contentString;
}
If ([elementName isinclutostring: @ "content"]) {
Content = contentString;
}
If ([elementName isEqualToString: @ "time"]) {
Time = contentString;
}
}

// The parsing document ends
-(Void) parserDidEndDocument :( NSXMLParser *) parser {
NSLog (@ "end of document Parsing ");
}
@ End


 

Excerpted from the heart of the cold and buried in the Northern Region

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.