如何使用Cocoa来写XML文件和读XML文件[部分属于转载]

前端之家收集整理的这篇文章主要介绍了如何使用Cocoa来写XML文件和读XML文件[部分属于转载]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

M文件

//
//  EDUCATIONDocument.m
//  learn_xml_readwrite
//
//  Created by on 24/7/14.
//  Copyright (c) 2014 EDUCATION. All rights reserved.
//

#import "EDUCATIONDocument.h"

@implementation EDUCATIONDocument

@synthesize m_ET_RecordCount;
@synthesize m_LB_Path;
@synthesize m_LB_Path_Caption;
@synthesize m_LB_ReadAFile;
@synthesize m_LB_ReadXMLFileValueCount;
- (id)init
{
    self = [super init];
    if (self)
    {
        // Add your subclass-specific initialization here.
        
    }
    return self;
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers,you should remove this method and override -makeWindowControllers instead.
    return @"EDUCATIONDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    [super windowControllerDidLoadNib:aController];
    m_ET_RecordCount.stringValue = @"5";
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
}

+ (BOOL)autosavesInPlace
{
    return YES;
}
- (IBAction)OnBT_ShowDialog_OpenAFile:(id)sender
{
    m_LB_Path_Caption.stringValue = @"Save File is :";
    m_LB_Path.stringValue = [self get_fullpath];
}

- (IBAction)OnBT_ShowDialog_SaveAFile:(id)sender
{
    m_LB_Path_Caption.stringValue = @"Save File is :";
    m_LB_Path.stringValue = [self get_save_fullpath:@"plist"];
    
}
// Full Version
- (IBAction)OnBT_SaveAFile:(id)sender
{
    m_LB_Path_Caption.stringValue = @"Save File is :";
    m_LB_Path.stringValue = [self get_save_fullpath:@"plist"];
    
    BOOL rs = [self save_xml:m_LB_Path.stringValue];
    if (YES==rs)
    {
        m_LB_ReadAFile.stringValue= @"Save Successfully!";
    }
    else
    {
        m_LB_ReadAFile.stringValue= @"Save Failed!";
    }
}

// Full Version
- (IBAction)OnBT_ReadAFile:(id)sender
{
    m_LB_Path_Caption.stringValue = @"Save File is :";
    m_LB_Path.stringValue = [self get_fullpath];
    m_LB_ReadAFile.stringValue = [self read_xml:m_LB_Path.stringValue];
}

-(NSString* )get_save_fullpath:(NSString *)file_extend
{
    NSString *fileName=@"";
    NSSavePanel *save = [NSSavePanel savePanel];
    long int result = [save runModal];
    if (result == NSOKButton)
    {
        NSString *selectedFile = [save filename];
        fileName = [[NSString alloc] initWithFormat:@"%@.%@",selectedFile,file_extend];
    }
    return fileName;
}

-(BOOL)save_xml:(NSString *)save_fullpath
{
    id m_clientName=nil;   // outlets to text Boxes in the preferences
    NSMutableDictionary * prefs;
    int icount= [m_ET_RecordCount.stringValue intValue];
    NSString *aa=@"";
    aa = [NSString stringWithFormat:@"%d",icount];
    
    m_clientName = aa.copy;
    //    NSString *aa_value;
    //    aa_value = m_clientName;
    
    // allocate an NSMutableDictionary to hold our preference data
    prefs = [[NSMutableDictionary alloc] init];
    
    // our preference data is our client name,hostname,and buddy list
    [prefs setObject:m_clientName forKey:@"record_count"];
    NSString *no=@"";
    NSString *id=@"";
    NSString *name=@"";
    
    NSString *caption_id=@"id";
    NSString *caption_name=@"name";
    
    int i=0;
    for (i=0; i<icount; i++)
    {
        id = [NSString stringWithFormat:@"%@%d",caption_id,i];
        name = [NSString stringWithFormat:@"%@%d",caption_name,i];
       
        no = [NSString stringWithFormat:@"%d",i];
        NSArray *classdata1 = [[NSArray alloc] initWithObjects:id,name,number,nil];
        [prefs setObject:classdata1 forKey:tel_no];
    }
    
    // save our buddy list to the user's home directory/Library/Preferences.
    BOOL bSaved;
    bSaved =[prefs writeToFile:[save_fullpath
                                stringByExpandingTildeInPath] atomically: TRUE];
    
    printf(@"save :%d",bSaved);
    return bSaved;
}

-(NSString *) read_xml:(NSString *)fullpath
{
    NSString *context=@"";
    NSDictionary *prefs;
    NSArray *classdata1 = [[NSArray alloc] init];

    // load the preferences dictionary
    prefs = [NSDictionary dictionaryWithContentsOfFile:
             [fullpath
              stringByExpandingTildeInPath]];
    
    if(prefs)
    {
        NSString* str_record_count = [prefs objectForKey:@"record_count"];
        
        int record_count = [str_record_count intValue];
        NSString *key_name=@"";
        
        int i_record_count=1;
        for (int i=1; i<record_count; i++)
        {
            key_name = [NSString stringWithFormat:@"%d",i];
            classdata1 = [[prefs objectForKey:key_name] retain];
            
            for (int i = 0; i < [classdata1 count]; i++)
            {
                NSLog(@"%@\n",[classdata1 objectAtIndex:i]);
                context =[context stringByAppendingFormat:@"%@",[classdata1 objectAtIndex:i]];
            }
            context =[context stringByAppendingFormat:@"%@",@"\n"];
            NSLog(@"----------\n");
            i_record_count++;
            m_LB_ReadXMLFileValueCount.stringValue = [NSString stringWithFormat:@"%d",i_record_count];
            
        }
        
    }
    return context;
}

-(NSString *)get_fullpath
{
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setMessage:@"select a file"];
    [panel setPrompt:@"OK"];
    [panel setCanChooseDirectories:YES];
    [panel setCanCreateDirectories:YES];
    [panel setCanChooseFiles:YES];
    
    NSString *path_all=@"";
    
    NSInteger result = [panel runModal];
    if (result == NSFileHandlingPanelOKButton)
    {
        path_all = [[panel URL] path];
    }
    return path_all;
}
@end


H文件

//
//  EDUCATIONDocument.h
//  learn_xml_readwrite
//
//  Created by  on 24/7/14.
//  Copyright (c) 2014 EDUCATION. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface EDUCATIONDocument : NSPersistentDocument
@property (assign) IBOutlet NSTextField *m_ET_RecordCount;
@property (assign) IBOutlet NSTextField *m_LB_Path;

@property (assign) IBOutlet NSTextField *m_LB_Path_Caption;

@property (assign) IBOutlet NSTextField *m_LB_ReadAFile;
@property (assign) IBOutlet NSTextField *m_LB_ReadXMLFileValueCount;

-(NSString* )get_save_fullpath:(NSString *)file_extend;
-(NSString *)get_fullpath;

-(BOOL)save_xml:(NSString *)save_fullpath;

-(NSString *)read_xml:(NSString *)fullpath;


@end


界面


测试结果:

成功!

原文链接:https://www.f2er.com/xml/298307.html

猜你在找的XML相关文章