Skip to content

Commit

Permalink
edit
Browse files Browse the repository at this point in the history
  • Loading branch information
ohsc authored and anandology committed Jan 5, 2011
1 parent 7c8397e commit 93a51ef
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cookbook/xmlfiles.zh-cn.md
@@ -0,0 +1,43 @@
---
layout: default
title: 提供XML访问
---

# 提供XML访问

### 问题

如何在web.py中提供XML访问?

如果需要为第三方应用收发数据,那么提供xml访问是很有必要的。

### 解法

根据要访问的xml文件(如response.xml)创建一个XML模板。如果XML中有变量,就使用相应的模板标签进行替换。下面是一个例子:

$def with (code)
<?xml version="1.0"?>
<RequestNotification-Response>
<Status>$code</Status>
</RequestNotification-Response>

为了提供这个XML,需要创建一个单独的web.py程序(如response.py),它要包含下面的代码。注意:要用"web.header('Content-Type', 'text/xml')"来告知客户端--正在发送的是一个XML文件。


import web

render = web.template.render('templates/', cache=False)

urls = (
'/(.*)', 'index'
)

app = web.application(urls, globals())

class index:
def GET(self, code):
web.header('Content-Type', 'text/xml')
return render.index(code)

web.webapi.internalerror = web.debugerror
if __name__ == '__main__': app.run()

0 comments on commit 93a51ef

Please sign in to comment.