Save/Restore GDataEntryCalendarEvent

Showing 1-8 of 8 messages
Save/Restore GDataEntryCalendarEvent KZ 9/25/10 7:34 AM
I'm beginer for both Objective-C and Google Data APIs.
I wish to use GDataEntryCalendarEvent with file.
How do i serialize GDataEntryCalendarEvent? And How do i restore
serialized GDataEntryCalendarEvent?
Re: Save/Restore GDataEntryCalendarEvent Greg Robbins 9/25/10 8:02 AM
Typically, applications will store data from inside entries, rather than entire entries or feeds. The library provides access to most of that data in standard Cocoa types (NSString, NSDate, and so on.)

You can serialize any GDataObject (feed, entry, or element) using XML. Do be aware that feeds returned by servers typically have namespaces for the elements defined only at the top of the feed, so to generate valid XML for just one entry from the feed, the full set of XML namespaces needs to be added to the entry.

Here's how to get XML for the first event entry from a calendar event feed:

  GDataEntryCalendarEvent *eventEntry = [calendarEventFeed firstEntry];
  // copy the parent feed's namespaces into this object
  [eventEntry setNamespaces:[eventEntry completeNamespaces]];
  NSXMLElement *element = [eventEntry XMLElement];
  NSString *xmlString = [element1 XMLString];

and how to regenerate the GDataObject from the XML:

  NSError *error = nil;
  NSXMLElement *element2 = [[[NSXMLElement alloc] initWithXMLString:xmlString
                                                              error:&error] autorelease];
  if (element2) {
    GDataEntryCalendarEvent *eventEntry2;
    eventEntry2 = [[[GDataEntryCalendarEvent alloc] initWithXMLElement:element2
                                                                parent:nil] autorelease];

Re: Save/Restore GDataEntryCalendarEvent KZ 9/25/10 12:38 PM
Greg, Thank you for your quick response.

I tried, but abort from that line.
>  // copy the parent feed's namespaces into this object
>  [eventEntry setNamespaces:[eventEntry completeNamespaces]];

I change
[eventEntry setNamespaces:[GDataEntryCalendarEvent

Is this right way?
Re: Save/Restore GDataEntryCalendarEvent Greg Robbins 9/25/10 1:31 PM
The -completeNamespaces method requires that the entry still be in a feed. 

You can also explicitly set the namespaces to the default set of calendar namespaces, as you did. That should work fine. Inspect the XML with

  NSLog(@"%@", [eventEntry XMLElement])

to confirm that the XML contains the needed namespace definitions.

Re: Save/Restore GDataEntryCalendarEvent KZ 9/26/10 4:26 AM
> The -completeNamespaces method requires that the entry still be in a feed.
I understand the reason and behavior.

Greg, Thank you for your explicit answer.
Re: Save/Restore GDataEntryCalendarEvent Matt Webster 7/19/11 10:11 AM
Thanks for the description, Greg.  I am using similar methods to save GDataEntryWorksheet (for spreadsheets) to disk and then retrieving it later.  Interestingly, I'm seeing an issue...

// writing to file (where spreadsheet is a GDataEntrySpreadsheet object)
        spreadsheet = [self selectedSpreadsheet];
        NSString *xmlRepresentation = [[spreadsheet XMLElement] XMLString];
        NSError *fileError = nil;
        BOOL ok = [xmlRepresentation writeToFile:spreadsheetPath atomically:YES encoding:NSUnicodeStringEncoding error:&fileError];
        if (!ok) {
            NSLog(@"Error writing file at %@\n%@",
                  spreadsheetPath, [fileError localizedFailureReason]);

// reading from file
        NSError *fileError = nil;
        NSString *xmlRepresentation = [[NSString alloc]
        if (fileError) {
            NSLog(@"Error reading file at %@\n%@",
                  spreadsheetPath, [fileError localizedFailureReason]);

        NSError *xmlError = nil;
        NSXMLElement *xmlElement = [[[NSXMLElement alloc] initWithXMLString:xmlRepresentation error:&xmlError] autorelease];
        if (xmlElement) {
            spreadsheet = [[[GDataEntrySpreadsheet alloc] initWithXMLElement:xmlElement parent:nil] autorelease];
        } else {
            NSLog(@"Error reading xml: %@", xmlError);

My xmlElement isn't getting set and I'm seeing the following error:

2011-07-19 13:06:36.482 SpreadsheetSample[88919:903] Error reading xml: Error Domain=NSXMLParserErrorDomain Code=201 UserInfo=0x3339e90 "Line 1: Namespace prefix gd for etag on entry is not defined

I think this is because the <entry etag> data I'm receiving from Google looks like this:
<entry gd:etag="&quot;GU1VQkhSACt7ImBr&quot;">

When I build xmlRepresentation manually and change the <entry etag> to:
<entry etag="&quot;GU1VQkhSACt7ImBr&quot;">

everything works as expected.  Being pretty new to iOS and GData, I am planning on parsing out the 'gd:' from xmlRepresentation.  Is there a better way to do this?


Re: Save/Restore GDataEntryCalendarEvent Matt Webster 7/19/11 10:55 AM
With a little help from stackoverflow, I did the following to pull out 'gd:' if it exists:

        NSRange rangeOfSubstring = [xmlRepresentation rangeOfString:@"gd:"];

        if (rangeOfSubstring.location != NSNotFound) {

            NSString *tmp = [NSString stringWithFormat:@"%@%@",

            [xmlRepresentation substringToIndex:rangeOfSubstring.location],

            [xmlRepresentation substringFromIndex:(rangeOfSubstring.location + 3)]];

            xmlRepresentation = tmp;


I'm sure I'm missing something about the GDataEntrySpreadsheet object but for the time-being, this works.

(and so I don't forget, the spreadsheetPath defined in my above post was...

NSString *spreadsheetPath = [NSHomeDirectory() stringByAppendingString:@"/mySpreadsheet.archive"];


Re: Save/Restore GDataEntryCalendarEvent Greg Robbins 7/19/11 1:31 PM
Entries inside feeds do not contain the declarations of XML namespaces declared in the feed, so those namespaces need to be added to the entries if the XML is stored.

But this discussion should be given a new subject, not tacked onto an old thread.