Search Posts in my Blog

Thursday 1 March 2012

Add Data inside xml file using GData parser



In this tutorial we will see how to add a node at runtime using GData xml parser, before beginning with the tutorial I would like to clear some things and that’s the resource group which is present in our application bundle has read only rights you cannot write in the bundle file so in this case what you need to do is copy the file in which you want to do operations into the document directory and then start manipulating that file via some file operations, and before beginning with this tutorial I would recommend that you read my GData xml parser post.

Design Phase: For this post I will not be having any design I would be displaying the output into the console.

Step 1: Create an xml file of your choice and drag it in the resource group, our first step would be to copy this file into the document directory, here's a snap of my xml file that i am using



Step 2: Please check my previous blog for GData and perform the initial settings for GData and then get back to this tutorial. Now once the settings are done we need three functions in order to add the data to the xml file present in the document directory
  • The first function is will help us to get the document directory path.
  • The second function will help us to copy the file from the bundle to the document directory and
  • The third function will help us to add the elements to the xml file which is copied from the bundle to the document directory.
Below given are first two function which you have already seen in my earlier demo of plist



-(NSString*)DocumentDirectoryPath
{
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES);
NSString *documentFolder = [documentPath objectAtIndex:0];
return documentFolder;
}


-(void)copyFileFromBundle_ToDocument_Directory
{
documentxmlFilePath = [[NSString alloc]init];
documentxmlFilePath = [[self DocumentDirectoryPathstringByAppendingPathComponent:@"Employee.xml"];
NSLog(@"The file name is %@",documentxmlFilePath);
NSString *bundlePath = [[NSBundle mainBundle]pathForResource:@"Employee" ofType:@"xml"];
//the below code copies the said file from the bundle into the document directory
[[NSFileManager defaultManagercopyItemAtPath:bundlePath toPath:documentxmlFilePath error:nil];
}


Step 3: In this step we will see the code snippet to add data into the xml file present in the doc directory


-(void)AddData
{
NSData *xmlData = [[NSDataalloc]initWithContentsOfFile:documentxmlFilePath];
xmlDocument = [[GDataXMLDocument alloc]initWithData:xmlData options:0error:nil];
GDataXMLElement *mainElement = [GDataXMLNodeelementWithName:@"Employee"];
//here you are creating some new nodes with values
GDataXMLElement *nameElement = [GDataXMLNodeelementWithName:@"name" stringValue:@"Rahul"];
GDataXMLElement *domainElement = [GDataXMLNodeelementWithName:@"domain" stringValue:@"Dot Net"];
GDataXMLElement *designationElement = [GDataXMLNodeelementWithName:@"designation" stringValue:@"Developer"];
//since employee is the parent node of the above node we need to add these values to it
[mainElement addChild:nameElement];
[mainElement addChild:domainElement];
[mainElement addChild:designationElement];
//ultimately employee element is the child element of the root element  
[xmlDocument.rootElement addChild:mainElement];
//you supply in the details of the new xml data that you want to write to the NSData variable
xmlData = xmlDocument.XMLData;
//finally write the data to the file in the doc directory
[xmlData writeToFile:documentxmlFilePath atomically:YES];
}


I have used the comments to explain you the code section 


Step 4: Into  the application didfinishlaunching method make a call to these two function and then see inside the document directory,


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
[self copyFileFromBundle_ToDocument_Directory];
[self AddData];
    [window makeKeyAndVisible];
    
    return YES;
}


if you are having trouble with finding your application folder then in that case just write the following code snippet so that you can find the your application in the folder 


NSLog(@"Doc dir path = %@",NSHomeDirectory());



Step 5: Press build and go and check out your xml file present in the document directory, here's the output of my xml file present in my doc directory




iHope that this post has helped you out on how to add data to xml file...

No comments:

Post a Comment