On Monday, 23 April 2012 12:03:49 UTC+5:30, Karra wrote:
>
>
> On Monday, 23 April 2012 06:28:08 UTC+5:30, Karra wrote:
>  
>
>> cElementTree.ParseError: unbound prefix: line 5, column 1552
>
>
> I just found this thread that suggests there is an uncaught parsing error 
> in the internal google code. Can someone from the Contacts team comment on 
> this, please?
>
> https://code.google.com/p/sync-google-contacts/issues/detail?id=5 
>

I guess this issue has been known since Aug 2011, but there has been no 
response from Google. But thank goodness, the code is open... I am able to 
apply the attached patch and move on. Posting here so others can benefit as 
well.

As noted in the comments, I have no idea why the invalid XML is being 
generated, and hence, what the real impact of the patch is. I guess I'll 
find out soon...

------

    Fix invalid contact entry XML from Google Data server
    
    Certain contacts - it is not entirely clear what sort of contacts -
    generate invliad XML from Google that the client libraries are
    unable to parse. This can cause an irrecoverable exception that looks
    like: "cElementTree.ParseError: unbound prefix: line 1, column 3089"
    
    The actual column number and line number may differ. We fix the problem
    by patching the incoming XML by editing out the invalid parts. This p
    atch is not extensively tested, but certainly allows life to move
    forward.

diff --git a/lib/atom/core.py b/lib/atom/core.py
index a19a70d..1da703a 100644
--- a/lib/atom/core.py
+++ b/lib/atom/core.py
@@ -21,6 +21,7 @@
 __author__ = '[email protected] (Jeff Scudder)'
 
 
+import string
 import inspect
 try:
   from xml.etree import cElementTree as ElementTree
@@ -517,6 +518,19 @@ def parse(xml_string, target_class=None, version=1, 
encoding=None):
       xml_string = xml_string.encode(STRING_ENCODING)
     else:
       xml_string = xml_string.encode(encoding)
+
+  # For some contacts, the google data protocol returns invalid XML. This
+  # issue was originally reported on the python client api google groups, 
but
+  # it essentially an underlying data api error. More on this error at:
+  # https://code.google.com/p/gdata-python-client/issues/detail?id=538
+  #
+  # This is a fatal error and applications cannot proceed when this error
+  # occurs. The following hack patches the received xml and allows life to
+  # go on. Ofcourse, there is really no confirmation yet from Google on 
why we
+  # are getting invalid XML for certain contacts in the first place.
+  xml_string = string.replace(xml_string, r'<ns0:cc>', '')
+  xml_string = string.replace(xml_string, r'</ns0:cc>', '')
+
   tree = ElementTree.fromstring(xml_string)
   return _xml_element_from_tree(tree, target_class, version)
 

-- 
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html

Reply via email to