I created a client-server restaurant project in which I read from an XML file. But I need to write and update the XML file. I faced a lot of problem when I write XML . I searched various websites and forums, but I could not found any solutions. Please help me if anyone knows how to do this.
I wrote the following code to create a sample project which I found from a tutorial. But some errors occur when I build:
Error:NSXMLDocument , NSXmlNode undeclared
I tried adding the Cocoa framework to solve this, but it didn't seem to work.
- (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];
}
I know this is an old post, but might help someone.
<Cocoa/Cocoa.h>
is not supported in iOS.
C out KissXML. Add it to your project, and just change the NSXMLElement
to DDXMLElement
in the above code.
Here is Sample code in iOS Reference Library.
SeismicXML
XMLPerformance
精彩评论