【发布时间】: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