3

I'm trying to parse a google calendar feed like this:

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<p id="entries"></p>

<script>

$(document).ready(function(){
        $.ajax({
              url: 'http://www.google.com/calendar/feeds/339if8qgnu8pic5a2ru4moe5m0%40group.calendar.google.com/public/full',
              dataType: 'xml',
              success: parseXML
        });
        function parseXML(xml){
              $(xml).find('entry').each( function() {
                    $('#entries').append($(this).find('title').text() + '<br />')
              });
        }
});
</script>

</body>
</html>

But it won't find any elements. What am I doing wrong? Tried the same code with a simplier XML file which worked...

Niclas Nilsson
  • 5,691
  • 3
  • 30
  • 43
  • Could it be that the xml is not properly constructed? I opened the link with Chrome and usually Chrome parses xml documents and tabs them properly. It didn't this time... – C0D3 Mar 16 '12 at 14:12
  • Seems strange if Google themself are doing it wrong. But who knows? – Niclas Nilsson Mar 16 '12 at 14:12
  • I copied and pasted the xml into notepad++ which has nifty xml plugins, when I asked it to check XML, it said: XML Parsing error at line 2: Input is not proper UTF-8, indicate encoding! Bytes: 0xF6 0x72 0x73 0x61 – C0D3 Mar 16 '12 at 14:16
  • Oh. ok. Maybe it's the data that I added in the calendar that is wrong. But I thought google or firefox would have done that right... :-/ – Niclas Nilsson Mar 16 '12 at 14:31
  • Well, its not necessarily firefox. It's google or maybe the way you added data. I haven't dealt with google calendar api. You can also try taking out chunks of that xml, run your script again and repeat taking out parts to find out exactly where the error is. This error: XML Parsing error at line 2: Input is not proper UTF-8, indicate encoding! Bytes: 0xF6 0x72 0x73 0x61 doesn't make much sense to me either. Hope I was able to help. – C0D3 Mar 16 '12 at 14:59
  • I'm not saying you are wrong. But it seems a bit odd when I can't get this error myself. Emacs tells me nothing, the w3c validator, with utf-8 option on, says nothing and so on. I'm tending to believe the problem is something else. But I'm not sure. Checked the http header, it also says utf-8. – Niclas Nilsson Mar 16 '12 at 18:30
  • I also stripped out all swedish charchters from this test calendar, but it did'nt seem to help... – Niclas Nilsson Mar 16 '12 at 18:38

1 Answers1

1

When I tested myself, it seemed that it wouldn't find any elements, because there wasn't any data returned by the request. I thought that surely I'm missing something, due to the comments about invalid encoding... but, I downloaded the XML document and saved it next to the HTML file you provided, having made the following change:

url: 'full.xml',
Having done this, (for me) the paragraph element is now filled with the two entries that could be seen in the XML you were targeting. Surely this is because of the cross-domain policies and what-not?

You have multiple options on grabbing the XML properly:
  • Server-side PHP file_get_contents() / cURL
    >>The file can now be AJAXed by your page, as you're on the same domain.
    >>This server-side request could also be part of this page, i.e: When the page is being loaded, PHP/Whatever could echo it as a variable within the JavaScript, or in a hidden element.

  • Modified AJAX Request
    >>A modified AJAX request that will work on cross-domain requests. Useful as the server then won't be performing the request, which is perhaps what you desire. If this is the case, then I should mention; I have a file named jquery.xdomainajax.js which allows cross-domain AJAX requests. I am looking for the source at the moment, but I figured I would rush this post so that you may search yourself if you wish.

    Edit: Check this out.
    Edit2: After some brief and careless testing, I can't get it to work using the above jQuery workaround plugin... so my solution would be to scrape it server-side.

    Here are some more links:

  • How to load a certain div of an external webpage on another webpage that is on a different domain
  • Get Websites Title
  • JQuery ajax cross domain call and permission issue



FYI: I am using FireFox 12.0

Community
  • 1
  • 1
Spooky
  • 2,966
  • 8
  • 27
  • 41
  • Hi! Thanks a lot for you caring about this still. It was a while since i worked with this. But I started of with scraping it server side and it still works. But my problem is, that in the real app I scrape three different calendars and it makes the page load unnecessarily slow. So i thought by scraping it client side. But I guess I will end up having some jQuery fetch data from a php page after the main page has loaded... – Niclas Nilsson May 16 '12 at 14:02
  • 1
    Yeah, the best idea in that case would probably be to load the page and AJAX to a PHP page that will return the scraped XML, as you said. I'm glad you figured it out :D – Spooky May 16 '12 at 14:53