有谁知道为什么文件在第 9 行的“<taskinprogress>”行格式不正确。它将“<project>”与“<taskinprogress>”放在同一行,我不知道为什么。我希望将“<project>”移到下一行(不向字符串添加 \n 字符)。
<client_state>
<platform>******PLATFORMNAME*******</platform>
<OS_version>********OS VERSION*******</OS_version>
<client_version>********client VERSION*******</client_version>
<power_supply_connected>********POWER SUPPLY CONNECTED*******</power_supply_connected>
<work_mode>********Work Mode*******</work_mode>
<battery_level>********Battery Level*******</battery_level>
<last_update>********Last Update*******</last_update>
<task_in_progress>********task_in_progress*******<project>********Project*******</project>
<application>********Application*******</application>
<xml_file>********xml_file*******</xml_file>
<task_id>********task_id*******</task_id>
<create_time>********create_time*******</create_time>
<receive_time>********receive_time*******</receive_time>
<input>********input*******</input>
</task_in_progress>
</client_state>
以下是我声明所有节点和元素的代码。
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSXMLElement *client_state = [[NSXMLElement alloc] initWithName:@"client_state"];
NSXMLElement *platform = [[NSXMLElement alloc] initWithName:@"platform"];
[platform setStringValue:@"******PLATFORMNAME*******"];
[client_state addChild:platform];
[platform release];
NSXMLElement *OS_version = [[NSXMLElement alloc] initWithName:@"OS_version"];
[OS_version setStringValue:@"********OS VERSION*******"];
[client_state addChild:OS_version];
[OS_version release];
NSXMLElement *client_version = [[NSXMLElement alloc] initWithName:@"client_version"];
[client_version setStringValue:@"********client VERSION*******"];
[client_state addChild:client_version];
[client_version release];
NSXMLElement *power_supply_connected = [[NSXMLElement alloc] initWithName:@"power_supply_connected"];
[power_supply_connected setStringValue:@"********POWER SUPPLY CONNECTED*******"];
[client_state addChild:power_supply_connected];
[power_supply_connected release];
NSXMLElement *work_mode = [[NSXMLElement alloc] initWithName:@"work_mode"];
[work_mode setStringValue:@"********Work Mode*******"];
[client_state addChild:work_mode];
[work_mode release];
NSXMLElement *battery_level = [[NSXMLElement alloc] initWithName:@"battery_level"];
[battery_level setStringValue:@"********Battery Level*******"];
[client_state addChild:battery_level];
[battery_level release];
NSXMLElement *last_update = [[NSXMLElement alloc] initWithName:@"last_update"];
[last_update setStringValue:@"********Last Update*******"];
[client_state addChild:last_update];
[last_update release];
//if(task in progress)
NSXMLElement *task_in_progress = [[NSXMLElement alloc] initWithName:@"task_in_progress"];
[task_in_progress setStringValue:@"********task_in_progress*******"];
[client_state addChild:task_in_progress];
[task_in_progress retain];
[task_in_progress release];
//start adding to inner task in progress
NSXMLElement *project = [[NSXMLElement alloc] initWithName:@"project"];
[project setStringValue:@"********Project*******"];
[task_in_progress addChild:project];
[project release];
NSXMLElement *application = [[NSXMLElement alloc] initWithName:@"application"];
[application setStringValue:@"********Application*******"];
[task_in_progress addChild:application];
[application release];
NSXMLElement *xml_file = [[NSXMLElement alloc] initWithName:@"xml_file"];
[xml_file setStringValue:@"********xml_file*******"];
[task_in_progress addChild:xml_file];
[xml_file release];
NSXMLElement *task_id = [[NSXMLElement alloc] initWithName:@"task_id"];
[task_id setStringValue:@"********task_id*******"];
[task_in_progress addChild:task_id];
[task_id release];
NSXMLElement *create_time = [[NSXMLElement alloc] initWithName:@"create_time"];
[create_time setStringValue:@"********create_time*******"];
[task_in_progress addChild:create_time];
[create_time release];
NSXMLElement *receive_time = [[NSXMLElement alloc] initWithName:@"receive_time"];
[receive_time setStringValue:@"********receive_time*******"];
[task_in_progress addChild:receive_time];
[receive_time release];
NSXMLElement *input = [[NSXMLElement alloc] initWithName:@"input"];
[input setStringValue:@"********input*******"];
[task_in_progress addChild:input];
[input release];
[task_in_progress release];
NSXMLDocument *xmlRequest = [NSXMLDocument documentWithRootElement:client_state];
[client_state release];
NSLog(@"XML Document\n%@", xmlRequest);
NSData *xmlData = [xmlRequest XMLDataWithOptions:NSXMLNodePrettyPrint];
[xmlData writeToFile:@"client_state.xml" atomically:YES];
[xmlRequest release];
return 0;
}