NSXMLParser Simple Example

后端 未结 4 606
说谎
说谎 2020-12-24 10:50

Most of the examples of how to invoke the NSXMLParser are contained within complex projects involving Apps. What does a simple example that demonstrates the callbacks look l

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-24 11:28

    #import 
    
    @interface AppDelegate : NSObject 
    @property (nonatomic, strong) NSMutableDictionary *dictXML;
    @property (nonatomic,strong) NSMutableArray *arrOfUpdateDictsByVersion;
    @property (nonatomic,strong) NSString *strElementBeingParsed;
    @property (nonatomic,strong) NSString *strElementFinishedParsing;
    
    @end
    
    
    #import "AppDelegate.h"
    
    @interface AppDelegate ()
    @property (weak) IBOutlet NSWindow *window;
    @end
    
    @implementation AppDelegate
    
    @synthesize dictXML;
    @synthesize arrOfUpdateDictsByVersion;
    
    @synthesize strElementBeingParsed;
    @synthesize strElementFinishedParsing;
    
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        // Insert code here to initialize your application
    
        [self initializeTheArray];
        [self startParsingXML];
    }
    
    -(void)initializeTheArray{
        self.dictXML = [[NSMutableDictionary alloc] init];
        self.arrOfUpdateDictsByVersion = [[NSMutableArray alloc] init];
    }
    
    -(void)startParsingXML{
    //    NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://cdn.example.com/databaseupdate.xml"]];
        NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL fileURLWithPath:@"/Users/vkrmsinha/Desktop/xmlParse.xml"]];
        [xmlparser setDelegate:self];
        [xmlparser parse];
    }
    
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
    {
        // initial tag comes in here
        self.strElementBeingParsed = elementName;
        /*if([elementName isEqualToString:@"Version"]){
    
        }
        else if ([elementName isEqualToString:@"Update"]){
    
        }*/
    }
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
    {
    
        if(([string rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location != NSNotFound) && ![self.strElementBeingParsed isEqualToString:@"Update"])
            return;
    
        // middle part from between the start and end tags comes here
        if ([self.strElementBeingParsed isEqualToString:@"Update"]){
            NSMutableDictionary *dictUpdate = [NSMutableDictionary dictionary];
            [self.arrOfUpdateDictsByVersion addObject:dictUpdate];
        }
        else if ([self.strElementBeingParsed isEqualToString:@"UpdateType"]){
            NSMutableDictionary *dictUpdate = [self.arrOfUpdateDictsByVersion lastObject];
            NSMutableDictionary *dictUpd = [NSMutableDictionary dictionary];
            [dictUpd setValue:string forKey:@"UpdateType"];
            [dictUpdate setValue:dictUpd forKey:@"update"];
        }
        else if([self.strElementBeingParsed isEqualToString:@"Version"]){
            NSMutableDictionary *dictUpdate = [self.arrOfUpdateDictsByVersion lastObject];
            // WARNING: ASK IF NO TWO VERSION WILL BE SAME IN FUTURE
            [dictUpdate setValue:string forKey:@"version"];
        }
        else if ([self.strElementBeingParsed isEqualToString:@"FileName"]){
            NSMutableDictionary *dictUpdate = [self.arrOfUpdateDictsByVersion lastObject];
            NSMutableDictionary *dict = [dictUpdate objectForKey:@"update"];
            [dict setValue:string forKey:@"FileName"];
        }
        else if ([self.strElementBeingParsed isEqualToString:@"Hash"]){
            NSMutableDictionary *dictUpdate = [self.arrOfUpdateDictsByVersion lastObject];
            NSMutableDictionary *dict = [dictUpdate objectForKey:@"update"];
            [dict setValue:string forKey:@"Hash"];
        }
        else if ([self.strElementBeingParsed isEqualToString:@"DownloadURL"]){
            NSMutableDictionary *dictUpdate = [self.arrOfUpdateDictsByVersion lastObject];
            NSMutableDictionary *dict = [dictUpdate objectForKey:@"update"];
            [dict setValue:string forKey:@"DownloadURL"];
        }
        else if ([self.strElementBeingParsed isEqualToString:@"Size"]){
            NSMutableDictionary *dictUpdate = [self.arrOfUpdateDictsByVersion lastObject];
            NSMutableDictionary *dict = [dictUpdate objectForKey:@"update"];
            [dict setValue:string forKey:@"Size"];
        }
    
    }
    
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
    {
        // ending of tag comes in here
        self.strElementFinishedParsing = elementName;
        if([elementName isEqualToString:@"Update"]){
            [self.arrOfUpdateDictsByVersion sortUsingDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"self.version" ascending:YES], nil]];
            NSLog(@"%@", [self.arrOfUpdateDictsByVersion lastObject]);
    
        }
        if([elementName isEqualToString:@"UpdateDetails"]){
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setValue:[[self.arrOfUpdateDictsByVersion lastObject] objectForKey:@"version"] forKey:@"latestVer"];
            [dict setValue:[[self.arrOfUpdateDictsByVersion firstObject] objectForKey:@"version"] forKey:@"oldestVer"];
            [dict setValue:self.arrOfUpdateDictsByVersion forKey:@"arrOfUpdsByVer"];
            NSLog(@"%@", dict);
        }
    
    }
    
    - (void)applicationWillTerminate:(NSNotification *)aNotification {
        // Insert code here to tear down your application
    }
    
    
    
       
        
         CompleteDatabase
         1
         1completedatabase.zip
         ad94431d2fe4cd60eb3347fadaa45d88
         
          http://www.example.com/download/new.xml
         
         2367008
        
    
    
    

提交回复
热议问题