[英]Generate XML document using python
i need to output my data in xml format like 我需要以xml格式输出数据,例如
<?xml version="1">
<lang name='en'>
<id>1</id>
</lang>
how can i do that ? 我怎样才能做到这一点 ?
<?xml version="1">
<lang name='en'>
<id>{{ id }}</id>
</lang>
And render it in the handler: 并在处理程序中呈现它:
class MainPage(webapp.RequestHandler):
def get(self):
id = 1
self.response.headers['Content-Type'] = "application/xml"
path = os.path.join(os.path.dirname(__file__), 'templates/index.xml')
self.response.out.write(template.render(path, {'id': id}))
Consider using ElementTree library. 考虑使用ElementTree库。 Example code: 示例代码:
from elementtree.ElementTree import Element
root_xml = Element("root")
root_xml.append(Element("test"))
# Get the output as xml:
output = etree.tostring(root_xml, pretty_print=True)
print output
if figured out how to solve my problem:- since app engine does not support standard Python XML Libraries i formatted a string by my self that output data as xml and that worked fine for me 如果想出了解决问题的方法:-由于应用程序引擎不支持标准的Python XML库,因此我自行格式化了一个字符串,该字符串将数据输出为xml,并且对我来说效果很好
xml = '<?xml version="1">\n<lang name="' + lang_name + '">\n <ID>' + data + '</ID>\n</lang>'
self.response.write(xml)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.