1

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;
}
4

1 に答える 1

1

私の愚かな間違い。私はそれを考え出した。他の誰かがこれを読んで興味を持っている場合は、すべてのルートタイプノードにも値があるべきではありません。「<task_in_progress」をノードとして使用しているので、宣言に続く行を削除する必要がありました。

//delete this:  Its a root, not a node w/ a value
 [task_in_progress setStringValue:@"********task_in_progress*******"];
于 2012-05-03T13:51:52.330 に答える