2
所以我必須在我看來這樣的功能:如何使用xml標籤將元素打印爲正確的xml?
from django.http import HttpResponse
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
def helloworld(request):
root_element = Element("root_element")
comment = Comment("Hello World!!!")
root_element.append(comment)
foo_element = Element("foo")
foo_element.text = "bar"
bar_element = Element("bar")
bar_element.text = "foo"
root_element.append(foo_element)
root_element.append(bar_element)
return HttpResponse(tostring(root_element), "application/xml")
它做什麼它打印是這樣的:
<root_element><!--Hello World!!!--><foo>bar</foo><bar>foo</bar></root_element>
正如你所看到的,它缺少在開始的XML標籤。如何從XML聲明開始輸出正確的XML?