-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLParser.m
451 lines (405 loc) · 11.4 KB
/
XMLParser.m
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//
// XMLParser.m
// scraper
//
// Created by michael on 08.01.15.
// Copyright (c) 2015 michael. All rights reserved.
//
#import "XMLParser.h"
@implementation XMLParser
//@synthesize movies;
//@synthesize actors;
NSMutableArray *movies;
NSMutableArray *actors;
NSMutableArray *producers;
NSMutableArray *thumbs;
NSMutableArray *fanarts;
NSMutableArray *genres;
NSMutableArray *countries;
NSMutableArray *credits;
NSMutableArray *directors;
//- (id) init {
// if (self = [super init]) {
// movies = [[NSMutableArray alloc] init];
// actors = [[NSMutableArray alloc] init];
// }
// return self;
//}
-(NSMutableArray *) getMovies {
return movies;
}
-(NSMutableArray *) getActors {
return actors;
}
-(NSMutableArray *) getThumbs {
return thumbs;
}
-(NSMutableArray *) getFanarts {
return fanarts;
}
-(Movie *) loadXMLByPath:(NSString *)pathString;
{
//NSString *path=@"/Users/michael/Desktop/Movies/Matrix/movie.nfo"; //this is ur path of xml file
NSString *path=pathString;
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
//NSURL *url = [[NSURL alloc] initFileURLWithPath:pathString];
NSError *error;
NSString * dataString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
parser = [[NSXMLParser alloc] initWithData:data ];
//parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
XMLParser *theDelegate = [[XMLParser alloc] init];
[parser setDelegate:theDelegate];
[parser parse];
// parser.delegate = self;
//[parser parse];
// NSLog(@"movies loadxml: %@", movies);
return [movies objectAtIndex:0];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
movies = [[NSMutableArray alloc] init];
actors = [[NSMutableArray alloc] init];
thumbs = [[NSMutableArray alloc] init];
fanarts = [[NSMutableArray alloc] init];
genres = [[NSMutableArray alloc] init];
countries = [[NSMutableArray alloc] init];
directors = [[NSMutableArray alloc] init];
producers = [[NSMutableArray alloc] init];
credits = [[NSMutableArray alloc] init];
currentNodeContent = nil;
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:@"movie"] && [attributeDict count] == 0 ) {
currentMovie = [[Movie alloc] init];
depth = 0;
} else if ([elementname isEqualToString:@"movie"] && [attributeDict count] != 0 ) {
depth = 1;
} else if ([elementname isEqualToString:@"recommendations"]) {
depth = 1;
} else if ([elementname isEqualToString:@"actor"]) {
currentActor = [[Actor alloc] init];
depth = 1;
} else if ([elementname isEqualToString:@"director"]) {
currentDirector = [[Actor alloc] init];
} else if ([elementname isEqualToString:@"credits"]) {
currentProducer = [[Actor alloc] init];
} else if (([elementname isEqualToString:@"thumb"]) && (depth == 0)) {
currentThumb = [[Thumb alloc] init];
NSString *attrAspect=[attributeDict valueForKey:@"aspect"];
NSString *attrPreview=[attributeDict valueForKey:@"preview"];
if (attrAspect) {
currentThumb.aspect = attrAspect;
}
if (attrAspect) {
currentThumb.preview = attrPreview;
}
depth = 2;
} else if ([elementname isEqualToString:@"fanart"]) {
currentFanart = [[Thumb alloc] init];
depth = 3;
} else if (([elementname isEqualToString:@"thumb"]) && (depth == 3)) {
NSString *attrAspect=[attributeDict valueForKey:@"aspect"];
NSString *attrPreview=[attributeDict valueForKey:@"preview"];
if (attrAspect) {
currentFanart.aspect = attrAspect;
}
if (attrAspect) {
currentFanart.preview = attrPreview;
}
//depth = 4;
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
currentNodeContent = (NSMutableString *) [currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[currentNodeContent stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
if ([elementname isEqualToString:@"title"])
{
currentMovie.title = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"originaltitle"])
{
currentMovie.originaltitle = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"rating"])
{
currentMovie.rating = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"year"])
{
currentMovie.year = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"top250"])
{
currentMovie.top250 = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"votes"])
{
currentMovie.votes = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"outline"])
{
currentMovie.outline = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"plot"])
{
currentMovie.plot = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"tagline"])
{
currentMovie.tagline = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"runtime"])
{
currentMovie.runtime = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"mpaa"])
{
currentMovie.mpaa = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"playcount"])
{
currentMovie.playcount = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"lastplayed"])
{
currentMovie.lastplayed = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"id"])
{
currentMovie.imdbid = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"tmdbid"])
{
currentMovie.tmdbid = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"tvdbid"])
{
currentMovie.tvdbid = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"itunesid"])
{
currentMovie.itunesid = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"set"])
{
currentMovie.set = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"premiered"])
{
currentMovie.premiered = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"status"])
{
currentMovie.status = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"code"])
{
currentMovie.code = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"aired"])
{
currentMovie.aired = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"studio"])
{
currentMovie.studio = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"dateadded"])
{
currentMovie.dateadded = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"copyright"])
{
currentMovie.copyright = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"trailer"])
{
currentMovie.trailer = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"kind"])
{
if ([currentNodeContent isEqualToString:@"9"]) {
currentMovie.kind = MP4MediaTypeMovie;
}
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"artistname"])
{
currentMovie.artistname = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"releasedate"])
{
currentMovie.releasedate = currentNodeContent;
currentMovie.year = [currentMovie.releasedate substringWithRange: NSMakeRange (0, 4)];
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"studio"])
{
currentMovie.studio = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"contentid"])
{
currentMovie.contentID = (long)currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"storeid"])
{
currentMovie.storeID = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"contentadvisoryrating"])
{
currentMovie.contentAdvisoryRating = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"mpaaitunes"])
{
currentMovie.mpaaiTunes = currentNodeContent;
currentNodeContent = nil;
}
// Genres
if ([elementname isEqualToString:@"genre"])
{
[genres addObject:currentNodeContent];
currentNodeContent = nil;
}
// Coutries
if ([elementname isEqualToString:@"country"])
{
[countries addObject:currentNodeContent];
currentNodeContent = nil;
}
// Directors
if ([elementname isEqualToString:@"director"])
{
currentDirector.name = currentNodeContent;
[directors addObject:currentDirector];
currentDirector = nil;
currentNodeContent = nil;
}
// Producers
if ([elementname isEqualToString:@"producer"])
{
currentProducer.name = currentNodeContent;
[producers addObject:currentProducer];
currentProducer = nil;
currentNodeContent = nil;
}
// Credits
if ([elementname isEqualToString:@"credits"])
{
currentProducer.name = currentNodeContent;
[producers addObject:currentProducer];
currentProducer = nil;
currentNodeContent = nil;
}
// Actors
if (([elementname isEqualToString:@"role"]) && (depth == 1))
{
currentActor.role = currentNodeContent;
currentNodeContent = nil;
}
if (([elementname isEqualToString:@"name"]) && (depth == 1))
{
currentActor.name = currentNodeContent;
currentNodeContent = nil;
}
if (([elementname isEqualToString:@"order"]) && (depth == 1))
{
currentActor.order = currentNodeContent;
currentNodeContent = nil;
}
if (([elementname isEqualToString:@"thumb"]) && (depth == 1))
{
currentActor.thumb = currentNodeContent;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"actor"])
{
[actors addObject:currentActor];
currentActor = nil;
currentNodeContent = nil;
depth = 0;
}
// Thumbs
if (([elementname isEqualToString:@"thumb"]) && (depth == 2))
{
currentThumb.value = currentNodeContent;
[thumbs addObject:currentThumb];
currentNodeContent = nil;
depth=0;
}
// Fanarts
if (([elementname isEqualToString:@"thumb"]) && (depth == 3))
{
currentFanart.value = currentNodeContent;
[fanarts addObject:currentFanart];
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"fanart"])
{
depth=0;
}
if ([elementname isEqualToString:@"recommendations"])
{
depth=0;
}
if (([elementname isEqualToString:@"movie"]) && (depth == 0))
{
currentMovie.actors = actors;
currentMovie.directors = directors;
currentMovie.producers = producers;
currentMovie.thumbs = thumbs;
currentMovie.fanarts = fanarts;
currentMovie.genres = genres;
currentMovie.countries = countries;
currentMovie.producers = credits;
[movies addObject:currentMovie];
currentMovie = nil;
currentNodeContent = nil;
}
currentNodeContent = nil;
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
//currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (!currentNodeContent) {
// init the ad hoc string with the value
currentNodeContent = [[NSMutableString alloc] initWithString:string];
} else {
// append value to the ad hoc string
[currentNodeContent appendString:string];
}
}
@end