如何使用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


界面


测试结果:

成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值