a="""<?xml version="1.0" encoding="utf-8"?>json
<catalog>dom
<maxid>4</maxid>post
<login username="pytest" passwd='123456'>測試
<caption>Python</caption>code
<item id="4">xml
<caption>測試</caption>utf-8
</item>get
</login>string
<item id="2">it
<caption>Zope</caption>
</item>
</catalog>
"""
import xml.dom.minidom
#打開文件用xml.dom.minidom.parse('a.xml')
dom = xml.dom.minidom.parseString(a)
root = dom.documentElement
bb = root.getElementsByTagName('caption')
b = bb[1]
uu = b.firstChild.data
print(uu)
a = root.getElementsByTagName('login')
a = a[0]
a = a.getAttribute('passwd')
print(a)
發下一個更好的:
def back(request): if request.method == 'POST': print(request) print(request.body) #print(request.raw_post_data) xml = request.body a = ObjectDict(dict((child.tag, child.text) for child in ET.fromstring(xml))) print(a) print(a.ToUserName) #print(a.Event) return HttpResponse(json.dumps(a)) return HttpResponse('hi')