简体   繁体   English

在Google Appengine中将数据导出为XML文件

[英]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. 我正在尝试将数据导出到Google appengine中的XML文件,正在使用Python / Django。 The file is expected to contain upto 100K records converted to XML. 该文件最多包含100K条记录转换为XML。 Is there an equivalent in App Engine of: App Engine中是否有以下等效项:

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). 编辑我要达到的目的是将一些信息导出到XML文档中,以便可以将其导出到google位置(不确切知道它如何工作,但是有人告诉我google会不时感染此xml文件时间)。

You could also generate XML with Django templates. 您还可以使用Django模板生成XML。 There's no special reason that a template has to contain HMTL. 模板不必包含HMTL,这没有特殊的原因。 I use this approach for generating the Atom feed for my blog. 我使用这种方法为我的博客生成Atom提要。 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. 我将传递给提要的帖子集合传递给它,每个Post实体都有一个to_atom方法,该方法生成其Atom表示形式。

<?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>adam@adamcrossland.net</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>

Every datastore model class has an instance method to_xml() that will generate an XML representation of that datastore type. 每个数据存储区模型类都有一个实例方法to_xml() ,该方法将生成该数据存储区类型的XML表示形式。

  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 适当设置响应的内容类型-如果要提示用户将文件保存在本地,请同时添加一个content-disposition标头
  3. generate whatever XML preamble you need to come before your record data 生成记录数据之前需要使用的任何XML序言
  4. iterate through the query results, calling to_xml() on each and adding that output to your reponse 遍历查询结果,在每个查询结果上调用to_xml()并将该输出添加到您的响应中
  5. do whatever closing of the XML preamble you need to do. 完成您需要做的XML序言的结尾。

What the author is talking about is probably Sitemaps . 作者谈论的大概是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. Sitemap是最简单的形式,它是一个XML文件,其中列出了站点的URL以及有关每个URL的其他元数据(相对于站点中其他URL,上一次更新的时间,通常更改的频率以及重要性) ),以便搜索引擎可以更智能地抓取该网站。

And about what I think you need is to write the XML to request object like so: 我认为您需要编写XML来请求对象,如下所示:

doc.writexml(self.response.out)

In my case I do this based on mime types sent from the client: 就我而言,我是根据客户端发送的mime类型来执行此操作的:

_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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM