Save/Restore GDataEntryCalendarEvent

58 views
Skip to first unread message

KZ

unread,
Sep 25, 2010, 10:34:03 AM9/25/10
to Google Data APIs Objective-C Client Library Discussion
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?

Greg Robbins

unread,
Sep 25, 2010, 11:02:03 AM9/25/10
to gdata-objec...@googlegroups.com
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];
  }

KZ

unread,
Sep 25, 2010, 3:38:06 PM9/25/10
to Google Data APIs Objective-C Client Library Discussion
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
calendarEventNamespaces]];

Is this right way?

Greg Robbins

unread,
Sep 25, 2010, 4:31:46 PM9/25/10
to gdata-objec...@googlegroups.com
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.

KZ

unread,
Sep 26, 2010, 7:26:20 AM9/26/10
to Google Data APIs Objective-C Client Library Discussion
> 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.

Matt Webster

unread,
Jul 19, 2011, 1:11:53 PM7/19/11
to gdata-objec...@googlegroups.com
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]
                                       initWithContentsOfFile:spreadsheetPath
                                       encoding:NSUnicodeStringEncoding
                                       error:&fileError];
        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?

Sincerely,
m@



Matt Webster

unread,
Jul 19, 2011, 1:55:24 PM7/19/11
to gdata-objec...@googlegroups.com
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"];

)

Greg Robbins

unread,
Jul 19, 2011, 4:31:31 PM7/19/11
to gdata-objec...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages