Python 如何在Flask中创建XML端点?
我在SQLAlchemy中具有以下属性:Python 如何在Flask中创建XML端点?,python,xml,python-2.7,flask,Python,Xml,Python 2.7,Flask,我在SQLAlchemy中具有以下属性: @property def serialize(self): return { 'name' : self.name, 'description' : self.description, 'id' : self.id, 'price' : self.price, 'course' : self.course, } 对于JSON,我刚刚使用了jsonify()
@property
def serialize(self):
return {
'name' : self.name,
'description' : self.description,
'id' : self.id,
'price' : self.price,
'course' : self.course,
}
对于JSON,我刚刚使用了
jsonify()
,如何处理XML?所有jsonify
都是使用JSON
转储传递给它的参数,并将响应的内容类型设置为application/JSON
。您可以对XML执行完全相同的操作:转储数据(Python有内置的库或功能更强大的库),并将内容类型设置为application/XML
有许多方法可以使用XML表示数据,因此这取决于您,但基本轮廓是:
import xml.etree.ElementTree as ET
root = ET.Element('root') # name the root whatever you want
# add your data to the root node in the format you want
return app.response_class(ET.tostring(root), mimetype='application/xml')