jordinl
jordinl

Reputation: 5239

Exporting data as an XML file in google appengine

I'm trying to export data to an XML file in the Google appengine, I'm using Python/Django. The file is expected to contain upto 100K records converted to XML. Is there an equivalent in App Engine of:

f = file('blah', 'w+')
f.write('whatever')
f.close()

?

Thanks

Edit What I'm trying to achieve is exporting some information to an XML document so it can be exported to google places (don't know exactly how this will work, but I've been told that google will fecth this xml file from time to time).

Upvotes: 1

Views: 1502

Answers (3)

soltysh
soltysh

Reputation: 1504

What the author is talking about is probably Sitemaps.

Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site.

And about what I think you need is to write the XML to request object like so:

doc.writexml(self.response.out)

In my case I do this based on mime types sent from the client:

_MIME_TYPES = {
    # xml mime type needs lower priority, that's needed for WebKit based browsers,
    # which add application/xml equally to text/html in accept header
    'xml':  ('application/xml;q=0.9', 'text/xml;q=0.9', 'application/x-xml;q=0.9',),
    'html': ('text/html',),
    'json': ('application/json',), 
}

mime = self.request.accept.best_match(reduce(lambda x, y: x + y, _MIME_TYPES.values()))
if mime:
    for shortmime, mimes in _MIME_TYPES.items():
        if mime in mimes:
            renderer = shortmime
            break
# call specific render function
renderer = 'render' + renderer
logging.info('Using %s for serving response' % renderer)
try:
    getattr(self.__class__, renderer)(self)
except AttributeError, e:
    logging.error("Missing renderer %s" % renderer)

Upvotes: 0

Adam Crossland
Adam Crossland

Reputation: 14213

You could also generate XML with Django templates. There's no special reason that a template has to contain HMTL. I use this approach for generating the Atom feed for my blog. The template looks like this. I pass it the collection of posts that go into the feed, and each Post entity has a to_atom method that generate its Atom representation.

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"
      xml:lang="en"
      xml:base="http://www.example.org">
  <id>urn:uuid:4FC292A4-C69C-4126-A9E5-4C65B6566E05</id>
  <title>Adam Crossland's Blog</title>
  <subtitle>opinions and rants on software and...things</subtitle>
  <updated>{{ updated }}</updated>
  <author>
    <name>Adam Crossland</name>
    <email>[email protected]</email>
  </author>
  <link href="http://blog.adamcrossland.net/" />
  <link rel="self" href="http://blog.adamcrossland.net/home/feed" />
  {% for each_post in posts %}{{ each_post.to_atom|safe }}
  {% endfor %}
</feed>

Upvotes: 1

bgporter
bgporter

Reputation: 36474

Every datastore model class has an instance method to_xml() that will generate an XML representation of that datastore type.

  1. Run your query to get the records you want
  2. Set the content type of the response as appropriate - if you want to prompt the user to save the file locally, add a content-disposition header as well
  3. generate whatever XML preamble you need to come before your record data
  4. iterate through the query results, calling to_xml() on each and adding that output to your reponse
  5. do whatever closing of the XML preamble you need to do.

Upvotes: 0

Related Questions