在Xcode中写入XML文件的问题

时间:2011-02-15 05:16:43

标签: iphone objective-c xml

我创建了一个客户端 - 服务器餐厅项目,我在其中读取了一个XML文件。但我需要编写和更新XML文件。我写XML时遇到了很多问题。 我搜索了各种网站和论坛,但我找不到任何解决方案。如果有人知道怎么做,请帮助我。

我编写了以下代码来创建一个我从教程中找到的示例项目。 但是当我构建时会出现一些错误:

  

错误:NSXMLDocument,NSXmlNode未声明

我尝试添加Cocoa框架来解决这个问题,但它似乎没有用。

- (NSData *)constructXMLRequest
{
    NSXMLElement *root = [[NSXMLElement alloc] initWithName:@"Request"];
    [root addAttribute:[NSXMLNode attributeWithName:@"Attribute1" stringValue:@"Value1"]];
    [root addAttribute:[NSXMLNode attributeWithName:@"Attribute2" stringValue:@"Value2"]];
    [root addAttribute:[NSXMLNode attributeWithName:@"Attribute3" stringValue:@"Value3"]];

    NSXMLElement *childElement1 = [[NSXMLElement alloc] initWithName:@"ChildElement1"];
    [root addChild:childElement1];
    [childElement1 release];

    NSXMLElement *childElement2 = [[NSXMLElement alloc] initWithName:@"ChildElement2"];
    [childElement2 addAttribute:[NSXMLNode attributeWithName:@"ChildAttribute2.1" stringValue:@"Value2.1"]];
    [childElement2 setStringValue:@"ChildValue2.1"];
    [root addChild:childElement2];
    [childElement2 release];

    NSXMLDocument *xmlRequest = [NSXMLDocument documentWithRootElement:root];
    [root release];
    NSLog(@"XML Document\n%@", xmlRequest);
    return [xmlRequest XMLData];
}

2 个答案:

答案 0 :(得分:3)

我知道这是一个老帖子,但可能对某人有帮助。

iOS不支持

<Cocoa/Cocoa.h>

C out KissXML。将其添加到您的项目中,只需在上面的代码中将NSXMLElement更改为DDXMLElement

答案 1 :(得分:0)

以下是iOS参考库中的示例代码。

SeismicXML

XMLPerformance