博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iphone开发之iphone解析xml&json-1
阅读量:5244 次
发布时间:2019-06-14

本文共 4670 字,大约阅读时间需要 15 分钟。

解析XML文件示例.

代码1和代码2是IOS开发的基本内容。

代码1.

#import 
@class XmlTestViewController;@interface XmlTestAppDelegate : NSObject
{ UIWindow *window; XmlTestViewController *viewController;}@property (nonatomic, retain) IBOutlet UIWindow *window;@property (nonatomic, retain) IBOutlet XmlTestViewController *viewController;@end

 

  代码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     didFinishLaunchingWithOptions:(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
 

代码3是controller的头文件。

 代码3

#import 
@interface XmlTestViewController : UIViewController {}@end

  controller的实现文件。在这声明了一个xml格式,NSString类型的字符串。并用代码5和代码6定义的类解析。

  代码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=@"
" "
标题" "
内容描述
" "
" "
"; 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

 

 代码5

#import 
@interface FirstXmlParse : NSObject
{ NSString *title; NSString *content; NSString *time; //定义一个可变字符串 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

  代码6.这是解析xml文件的关键内容。

#import "FirstXmlParse.h" @implementation FirstXmlParse @synthesize title; @synthesize content; @synthesize time; @synthesize contentString; -(void)startParse:(NSString *)xml{
NSData *data=[xml dataUsingEncoding:NSUTF8StringEncoding]; //创建xml解析器 NSXMLParser *xmlParse=[[NSXMLParser alloc] initWithData:data]; //设置委托 [xmlParse setDelegate:self]; [xmlParse parse]; } //解析文档开始 - (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@"解析文档开始"); } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
NSLog(@"遇到启始标签:%@",elementName); self.contentString=[NSMutableString string]; } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@"遇到内容:%@",string); [self.contentString appendString:string]; } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@"遇到结束标签:%@",elementName); if ([elementName isEqualToString:@"title"]) {
title=contentString; } if ([elementName isEqualToString:@"content"]) {
content=contentString; } if ([elementName isEqualToString:@"time"]) {
time=contentString; } } //解析文档结束 - (void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@"文档解析结束"); } @end

 

 

转载于:https://www.cnblogs.com/lucaszyl/archive/2012/03/26/2417499.html

你可能感兴趣的文章
使用C#交互快速生成代码!
查看>>
UVA11374 Airport Express
查看>>
P1373 小a和uim之大逃离 四维dp,维护差值
查看>>
NOIP2015 运输计划 树上差分+树剖
查看>>
P3950 部落冲突 树链剖分
查看>>
读书汇总贴
查看>>
微信小程序 movable-view组件应用:可拖动悬浮框_返回首页
查看>>
MPT树详解
查看>>
空间分析开源库GEOS
查看>>
RQNOJ八月赛
查看>>
前端各种mate积累
查看>>
jQuery 1.7 发布了
查看>>
Python(软件目录结构规范)
查看>>
Windows多线程入门のCreateThread与_beginthreadex本质区别(转)
查看>>
Nginx配置文件(nginx.conf)配置详解1
查看>>
linux php编译安装
查看>>
name phone email正则表达式
查看>>
「Unity」委托 将方法作为参数传递
查看>>
重置GNOME-TERMINAL
查看>>
redis哨兵集群、docker入门
查看>>