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


界面


测试结果:

成功!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇