读,修改,写可可的xml文件 [英] Read, modify, write xml file in cocoa

查看:99
本文介绍了读,修改,写可可的xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简单的例子/教程如何阅读,修改一个值和使用可可写一个XML文件。我发现的一切都是要简单(只是读取或写入)或复杂< a>(是一个完整的xml编辑器)。



这似乎是一个很标准的使用场景,所以我希望有东西在那里...

解决方案

我错了,当我声称写这样的元素不能做。
Apple有两个XML解析器,一个用于NSDictionary / NSArray / NSString / NSNumber / etc。对象和一个名为NSXMLDocument。



我删除了以前的代码,这是Mac OS X 10.3兼容;下面提到的代码将允许你有包含你喜欢的标签和属性的xml文件。
在我的示例中,代码将创建一个如下所示的XML文件:



< root>< counter> 1< / counter> ; / root>



注意:如果你想要进一步减少它,你甚至可以删除'counter'并将'root'重命名为'counter' >

新代码与Mac OS X 10.4和forward兼容;它已经过测试并开箱即用:

   - (void)incrementCounter 
{
NSXMLDocument * xmlDoc;
NSError * error;
NSURL * url;
NSXMLElement * root;
id item;
NSData * data;
NSArray * children;
int counter;
NSString * pathname;

pathname = [@〜/ myFilestringByExpandingTildeInPath];
url = [NSURL fileURLWithPath:pathname];

xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:url options:NSXMLDocumentTidyXML error:& error];
if(xmlDoc)
{
root = [xmlDoc rootElement];
}
else
{
root = [NSXMLNode elementWithName:@root];
xmlDoc = [[NSXMLDocument alloc] initWithRootElement:root];
}

//获取值:
children = [root nodesForXPath:@countererror:& error];
item = [children count]? [children objectAtIndex:0]:NULL;

//修改值:
counter = item? [[item stringValue] intValue]:0;
counter ++;
if(NULL == item)
{
item = [NSXMLNode elementWithName:@counterstringValue:@0];
[root insertChild:item atIndex:0];
}
[item setStringValue:[[NSNumber numberWithInt:counter] stringValue]];

// write:
data = [xmlDoc XMLData];
[data writeToURL:url atomically:YES];
}


I'm looking for a short example/tutorial on how to read, modify one value and write an xml file using cocoa. Everything that I found is either to simple (just read or write) or to complex (being a full xml editor).

This seems like a pretty standard usage scenario, so I'm hoping that there is something out there...

解决方案

I was in error, when I claimed that writing such elements couldn't be done. Apple have two XML parsers, one for NSDictionary/NSArray/NSString/NSNumber/etc. objects and one called NSXMLDocument.

I've removed the previous code, which was Mac OS X 10.3 compatible; the code mentioned below will allow you to have xml files containing whatever tags and attributes you like. In my example, the code will create an XML file that looks like the following:

<root><counter>1</counter></root>

Note: You can even remove the 'counter' and rename 'root' to 'counter', if you want to reduce it further.

The new code is compatible with Mac OS X 10.4 and forward; it's tested and works out-of-the-box:

- (void)incrementCounter
{
    NSXMLDocument   *xmlDoc;
    NSError         *error;
    NSURL           *url;
    NSXMLElement    *root;
    id              item;
    NSData          *data;
    NSArray         *children;
    int             counter;
    NSString        *pathname;

    pathname = [@"~/myFile" stringByExpandingTildeInPath];
    url = [NSURL fileURLWithPath:pathname];

    xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:url options:NSXMLDocumentTidyXML error:&error];
    if(xmlDoc)
    {
        root = [xmlDoc rootElement];
    }
    else
    {
        root = [NSXMLNode elementWithName:@"root"];
        xmlDoc = [[NSXMLDocument alloc] initWithRootElement:root];
    }

    // fetch value:
    children = [root nodesForXPath:@"counter" error:&error];
    item = [children count] ? [children objectAtIndex:0] : NULL;

    // modify value:
    counter = item ? [[item stringValue] intValue] : 0;
    counter++;
    if(NULL == item)
    {
        item = [NSXMLNode elementWithName:@"counter" stringValue:@"0"];
        [root insertChild:item atIndex:0];
    }
    [item setStringValue:[[NSNumber numberWithInt:counter] stringValue]];

    // write:
    data = [xmlDoc XMLData];
    [data writeToURL:url atomically:YES];
}

这篇关于读,修改,写可可的xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆