1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
AppDelegate.m #import "AppDelegate.h" #import "MainViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
MainViewController *mainVC = [[MainViewController alloc] init];
UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = naviVC;
[naviVC release];
[mainVC release];
[_window release];
return YES;
} - (void)dealloc { [_window release];
[ super dealloc];
} - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
} - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
} - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
} - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
} - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
} @end |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
MainViewController.m #import "MainViewController.h" #import "XMLSAXParser.h" #import "JSONParser.h" @interface MainViewController () @end @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [ super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad { [ super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor cyanColor];
//XML SAX解析(逐行解析)
XMLSAXParser *parser = [[XMLSAXParser alloc] init];
[parser startParse];
NSLog(@ "解析后的%@" ,parser.array);
[parser release];
} - (void)didReceiveMemoryWarning { [ super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
} */ @end |
1
2
3
4
5
6
7
8
|
XMLSAXParser.h #import <Foundation/Foundation.h> @interface XMLSAXParser : NSObject<NSXMLParserDelegate> @property (nonatomic , retain)NSMutableArray *array; //装学生对象
@property (nonatomic , retain)NSString *tempStr; //临时健在节点内容
- (void)startParse; //开始解析,逐行解析
- (void)startDOMParse; //开始dom解析,按节点解析
@end |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
XMLSAXParser.m #import "XMLSAXParser.h" #import "Student.h" #import "GDataXMLNode.h" @implementation XMLSAXParser //XML解析:逐行解析 - (void)startParse; { //XML解析:逐行解析
//从文件列表中读取数据
//1.获取文件路径
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@ "Student" ofType:@ "xml" ];
//2.通过路径把文件转换成NSData类型
NSData *data = [NSData dataWithContentsOfFile:sourcePath];
//创建的时候需要给parser一个字符串数据(NSData)
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
//设定代理人
parser.delegate = self;
//开始对文件进行解析
[parser parse];
//内存管理
[parser release];
} - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { //当找到节点头的时候,体统调用这个方法
NSLog(@ "节点头" );
if ([elementName isEqualToString:@ "students" ]) {
//当找到students节点头的时候,初始化数组
self.array = [NSMutableArray array];
} else if ([elementName isEqualToString:@ "student" ]){
//当找到student节点时候,创建一个新的学生对象,添加到数组中
Student *stu = [[Student alloc] init];
[self.array addObject:stu];
[stu release];
}
} - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { //当找到节点内容的时候,调用
NSLog(@ "节点内容" );
//把内容保存起来,只要碰到节点内容,下一个肯定是节点尾
self.tempStr = string;
} - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { //当找到节点尾时候调用
NSLog(@ "节点尾" );
//取得之前保存的student对象
Student *stu = [self.array lastObject]; //核心代码,懂了没 .........
if ([elementName isEqualToString:@ "name" ]) {
stu.name = self.tempStr;
} else if ([elementName isEqualToString:@ "sex" ]){
stu.sex = self.tempStr;
} else if ([elementName isEqualToString:@ "phone" ]){
stu.phone = self.tempStr;
} else if ([elementName isEqualToString:@ "number" ]){
stu.number = self.tempStr;
}
} //开始dom解析,按节点解析 - (void)startDOMParse { //按节点解析
//1.获取要解析文件的文件信息
NSString *xmlPath = [[NSBundle mainBundle]pathForResource:@ "Student" ofType:@ "xml" ];
NSData *data = [NSData dataWithContentsOfFile:xmlPath];
//参数1:要解析的xml串
//参数2:随便
//参数3:错误信息
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
//2.获取文件的根节点
GDataXMLElement *rootElement = [doc rootElement];
//3.进一步搜索所有的子节点
//返回一个装满了student节点(GDataXMLElement对象)的数组
NSArray *array = [rootElement elementsForName:@ "student" ];
self.array = [NSMutableArray array];
//4.遍历数组,把student节点的每个子节点取出来
for (GDataXMLElement *element in array) {
Student *stu = [[Student alloc] init];
GDataXMLElement *nameElement = [[element elementsForName:@ "name" ]lastObject];
//从name节点中取值
NSString *name = [nameElement stringValue];
//给学生对象的属性赋值
stu.name = name;
//把学生对象添加到数组中-----------其他的雷同
[self.array addObject:stu];
[stu release];
}
} @end |
1
2
3
4
5
6
7
8
9
|
Student.h #import <Foundation/Foundation.h> @interface Student : NSObject @property (nonatomic , retain)NSString *number; @property (nonatomic , retain)NSString *name; @property (nonatomic , retain)NSString *sex; @property (nonatomic , copy)NSString *phone; @end |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#import "Student.h" @implementation Student - (void)dealloc { [_name release];
[_number release];
[_sex release];
[_phone release];
[ super dealloc];
} //当这个类的对象被NSLog输出时候,系统会先调用这个方法,如果这个方法被重写,就直接输出重写的内容,否则就输出系统默认的内容 - (NSString *)description { return [NSString stringWithFormat:@ "name:%@ sex:%@ phone:%@ number:%@" , self.name, self.sex, self.phone, self.number];
} @end |
1
2
3
4
5
6
7
8
|
<?xml version= "1.0" encoding= "UTF-8" ?>
<employees> <employee> <name>ddviplinux</name> <sex>m</sex> <age>30</age> </employee> </employees> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public void parserXml(String fileName) { try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(fileName);
NodeList employees = document.getChildNodes();
for (int i = 0; i < employees.getLength(); i++) {
Node employee = employees.item(i);
NodeList employeeInfo = employee.getChildNodes();
for (int j = 0; j < employeeInfo.getLength(); j++) {
Node node = employeeInfo.item(j);
NodeList employeeMeta = node.getChildNodes();
for (int k = 0; k < employeeMeta.getLength(); k++) {
System.out.println(employeeMeta.item(k).getNodeName() + ":" + employeeMeta.item(k).getTextContent());
}
}
}
System.out.println( "解析完毕" );
} catch (Exception e) {
System.out.println(e.getMessage());
}
} |
1
2
3
4
5
6
7
8
9
10
|
public void parserXml(String fileName) { SAXParserFactory saxfac = SAXParserFactory.newInstance();
try {
SAXParser saxparser = saxfac.newSAXParser();
InputStream is = new FileInputStream(fileName);
saxparser.parse(is, new MySAXHandler());
} catch (Exception e) {
e.printStackTrace();
}
} |
相关推荐
Java 解析 XML 文件的 DOM 和 SAX 方式是指在 Java 中使用 Document Object Model(DOM)和 Simple API for XML(SAX)两种方式来解析 XML 文件的技术。下面将详细介绍这两种方式的原理、实现和优缺点。 DOM 方式 ...
在 XML 文件解析中,主要有两种解析方式:DOM(Document Object Model)和 SAX(Simple API for XML)。DOM 是一种基于树的解析方式,将 XML 文件解析成一个树状结构,然后对树中的节点进行操作。SAX 是一种基于事件...
DOM 是一种基于树或基于对象的 XML 解析技术,它是用与平台和语言无关的方式表示 XML 文档的官方 W3C 标准。DOM 将 XML 文档表示为一个层次结构的节点或信息片断的集合,允许开发人员在树中寻找特定信息。分析该结构...
解析 XML 通常有两种方式,DOM 和 SAX: DOM解析XML时,读入整个XML文档并构建一个驻留内存的树结构(节点树),通过遍历树结构可以检索任意XML节点,读取它的属性和值。而且通常情况下,可以借助XPath,直接查询XML...
用Java解析XML文档,最常用的有两种方法:使用基于事件的XML简单API(Simple API for XML)称为SAX和基于树和节点的文档对象模型(Document Object Module)称为DOM。Sun公司提供了Java API for XML Parsing(JAXP)...
在JAVA中有两种常见的XML解析方式,DOM和SAX,DOM在解析的时候会将所有的数据一次性载入内存中进行解析,在数据量比较大的情况下,效率非常低.尤其在手机这种对内存和性能要求比较苛刻的设备里面这种方法并不可取. ...
其次,JDOM 库支持多种 XML 解析方式,包括 SAX、DOM 和 XPath 等。最后,JDOM 库提供了高效的解析性能,能够快速地解析大型 XML 文件。 使用 JDOM 库解析 XML 文件是一种简单易用的方式,能够快速地解析和处理 XML...
* 高性能:dom4j使用SAX机制来解析XML文件和字符串,从而提高了解析速度。 * 强大功能:dom4j提供了完整的XML解析功能,包括读取、写入、遍历和修改XML文档。 * 易用性:dom4j的API设计简单易用,易于理解和使用。 ...
对于需要自己编写代码来处理XML文档的开发人员来说, 选择DOM还是SAX解析模型是一个非常重要的设计决策。 DOM采用建立树形结构的方式访问XML文档,而SAX采用的事件模型。 DOM解析器把XML文档转化为一个包含其内容...
1、【DOM】 DOM是基于树的结构,通常需要加载整文档和构造DOM树,然后才能开始工作。 优点: a、由于整棵树在内存中,因此可以对xml文档随机访问 ...这4中xml解析方式中,最优秀的一个,集易用和性能于一身。
Symbian已经为我们提供了类CParser在Symbian平台上对XML文件进行解析,Symbian中的CParser是基于SAX(SAX,Simple API for XML)方式的,它是基于事件流方式,与DOM相比SAX方式在操作上会有点麻烦而且显得没那么好理解...
Java 解析 XML 文档的常用方法有四种:DOM、SAX、JDOM 和 DOM4J。下面对这四种方法进行详细的介绍: 1. 使用 DOM(JAXP Crimson 解析器) DOM(Document Object Model)是一种以层次结构组织的节点或信息片断的...
本文档详细的给出了XML的介绍和XML解析的实例。包括DOM4J和SAX解析,节点的名和值得读取,属性的读取。生成XML文件等
解析XML的方式有很多种,大家比较熟悉的可能就是DOM解析。 DOM(文件对象模型)解析:解析器读入整个文档,然后构建一个驻留内存的树结构,然后代码就可以根据DOM接口来操作这个树结构了。 优点:整个文档读入内存,...
解析 XML 通常有两种方式,DOM 和 SAX: DOM解析XML时,读入整个XML文档并构建一个驻留内存的树结构(节点树),通过遍历树结构可以检索任意XML节点,读取它的属性和值。而且通常情况下,可以借助XPath,直接查询XML...
// 把要解析的XML文档转化为输入流,以便DOM解析器解析它 InputStream is = new FileInputStream("test.xml"); // 解析XML文档的输入流,得到一个Document Document doc = dombuilder.parse(is); // 得到XML...
在Python中解析XML文件也有Dom和Sax两种方式,这里先介绍如何是使用Dom解析XML,这一篇文章是Dom生成XML文件,下一篇文章再继续介绍Dom解析XML文件。 在生成XML文件中,我们主要使用下面的方法来完成。 主要方法 1、...
我们无须了解SAX和DOM的解析过程,只要给Digester添加一些解析规则,就能对一个xml文件进行解析。Digester使用堆栈来保存xml节点(stack.push()方法),当该xml节点中嵌套的所有子节点解析完毕,该节点将被弹出...
XML的解析有三种方式,在Android中提供了三种解析XML的方式:DOM(Document Objrect Model) , SAX(Simple API XML) ,以及Android推荐的Pull解析方式,他们也各有弊端,而这里来看看使用DOM的方式。 2. Dom解析 DOM解析...