通过NSXMLParser来解析XML
创建一个名为MyXML.xml文件
<?xml version = "1.0" encoding = "UTF-8" ?>
<root>
<person id = "1">
<firstName>Anthony</firstName>
<lastName>Robbins</lastName>
<age>51</age>
</person>
<person id = "2">
<firstName>Richard</firstName>
<lastName>Branson</lastName>
<age>61</age>
</person>
</root>
定义一个NSXMLParser类型的属性
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate :
UIResponder <UIApplicationDelegate,NSXMLParserDelegate>
@property (strong,
nonatomic) UIWindow *window;
@property (strong,
nonatomic) ViewController *viewController;
@property (strong,
nonatomic) NSXMLParser *xmlParser;
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
NSString *xmlFilePath = [[NSBundle
mainBundle]pathForResource:@"MyXML"
ofType:@"xml"];
NSData *xml = [[NSData
alloc]initWithContentsOfFile:xmlFilePath];
xmlParser = [[NSXMLParser
alloc]initWithData:xml];
xmlParser.delegate =
self;
if([xmlParser
parse]){
NSLog(@"The XML is parsed");
}else{
NSLog(@"Failed to parse the XML");
}
self.window = [[[UIWindow
alloc] initWithFrame:[[UIScreen
mainScreen] bounds]]
autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController
alloc] initWithNibName:@"ViewController"
bundle:nil]
autorelease];
//self.window.rootViewController = self.viewController;
UINavigationController *nav = [[UINavigationController
alloc]initWithRootViewController:self.viewController];
[self.window
addSubview:nav.view];
[self.window
makeKeyAndVisible];
return
YES;
}
首先把文件内容读取到一个NSData实例对象中,然后使用initWithData:来初始化我们的XML parser,并把我们从xml文件中读取出来的数据传进去。之后我们可以调用XML parser的parser方法来开始解析处理。这个方法会zus当前线程,直至解析处理结束。
parserDidStartDocument:解析开始的时候调用该方法。
parserDidEndDocument:解析结束的时候调用该方法。
parser:didStartElement:namespaceURI:qualifiedName:attributes:在XML document中,当解析器在解析的时候遇到了一个新的element时会被调用该方法。
parser:didEndElement:namespaceURI:qualifiedName:当前节点结束之后会调用。
parser:foundCharacters:当解析器在解析文档内容的时候被调用。
分享到:
相关推荐
ios开发,简单的xml解析,亲测可用,自己认真研究研究,不要遇到点问题就放弃。
xml解析的两种常用方式, DOM解析:一次性将整个XML文档加载进内存,比较适合解析小文件,例如:GDataXml解析 SAX解析:从根元素开始,按顺序一个元素一个元素往下解析,比较适合解析大文件,例如:NSXMLParser解析
Tinyxml iOS xml 解析 Tinyxml iOS xml 解析
NULL 博文链接:https://android-zhang.iteye.com/blog/1758353
iOS xml 解析 本来是喜欢用json的,可服务端的那位大神居然搞不出来 他说他是直接返回一串字符串的,是底层给疯装成xml返回的 嗨,累 网上搜了下,有说用sdk自带的NSXMLParse, 有说用google提供的GDataXML, 还有...
ios中xml解析demo 提供几种常用方法 运行后在后台打出
ios小程序--解析xml文件,读取新闻
见博客:http://blog.csdn.net/baidu_nod/article/details/36515883
XMLReader,XML解析,用于soap请求返回XML数据的解析
iOS xml解析和json解析demo
ios应用源码之对xml文件的解析与导出数据存储在xml中(代码)xmlparser
ios学习笔记中用到的美化按钮的背景图,iphone风格
ios学习笔记---simpleping 适合初学者练习使用。只是一个小的demo
IOS应用源码——XML解析.zip
iOS的XML解析库 TouchXML ,TouchXML 是使用 Objective-C 编写的 XML 解析器,适合在 iOS 和 Mac...
比原生的解析要易用很多,简单效率,是解析XML的很好的选择,推荐大家使用
是之前 IOS学习笔记上补充了一些 这边只是备份以后用到好下载 也欢迎你们来下载 学习 ^ ^
该demo中描述了2种解析模式,一是苹果自带的,二是TBXMl解析,代码中注释相当的详细啊,如果TBXML解析 有问题 请对照我博客ios xml解析,上面写得相当清楚
IOS应用源码之对xml文件的解析与导出数据存储在xml中(代码)xmlparser .zip
解析xml文件的通用类,使用libxml实现边下载边解析,下载使用ASI开源库. 可以设置最外边需要解析的tag