【问题标题】:Problems writing to an XML file in Xcode在 Xcode 中写入 XML 文件的问题
【发布时间】:2012-04-06 17:22:32
【问题描述】:

我创建了一个从 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];
}

【问题讨论】:

标签: iphone objective-c xml


【解决方案1】:

我知道这是一篇旧帖子,但可能会对某人有所帮助。

<Cocoa/Cocoa.h> 在 iOS 中不受支持。

退出KissXML。将其添加到您的项目中,只需将上述代码中的NSXMLElement 更改为DDXMLElement

【讨论】:

  • 谢谢!并且应该提到的是,甚至没有必要重命名类,因为如果平台是 iOS,KissXML 的标准头 #defined NSXMLElement as DDXMLElement (etc.)。
  • 遗憾的是,它并没有涵盖完整的 NSXML API。
  • 是的,它甚至没有documentWithRootElement 方法。避免它的可能解决方案groups.google.com/forum/#!topic/kissxml/MZ18szX_U8o
【解决方案2】:

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

SeismicXML

XMLPerformance

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多