python如何向页面传xml格式的数据?

Python018

python如何向页面传xml格式的数据?,第1张

要先说明后台的web框架是什么,

如果是简单的cgi:

print "Content-Type: text/xml"

print "xml字符串"就可以了。

使用Django作为web框架:

from django.http import HttpResponse

def 请求时调用的方法(request):

xml = "xml字符串"

return HttpResponse(xml)

你这个xml不标准,真正的xml需要最外面有一个<aa>和</aaa>包裹着,如果是标准的,可用如下

id = 1

import xml.dom.minidom

dom = xml.dom.minidom.parse("data.xml")

for i in dom.getElementsByTagName('pattern'):

if(i.childNodes[1].firstChild.data == str(id)): print i.toxml()