Question

Does anyone know why the file doesn't format correctly on the line "< taskinprogress>" its the 9th line. It puts "< project>" on the same line as "< taskinprogress> and I have no clue why. I want the "< project>" to be moved to the next line (without adding a \n character to a string).

<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>

The following is my code that declares all of the nodes and elements.

#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;
}
Was it helpful?

Solution

Silly mistake on my part. I figured it out. If anyone else is reading this and curious, all root type nodes should NOT also have a value. I am using "< task_in_progress" as a node, so I had to remove the line follow its declaration:

//delete this:  Its a root, not a node w/ a value
 [task_in_progress setStringValue:@"********task_in_progress*******"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top