pyhon处理xml格式数据

前端之家收集整理的这篇文章主要介绍了pyhon处理xml格式数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

a="""<?xml version="1.0" encoding="utf-8"?>

<catalog>

<maxid>4</maxid>

<login username="pytest" passwd='123456'>

  <caption>Python</caption>

<item id="4">

<caption>测试</caption>

</item>

</login>

<item id="2">

<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)


发下一个更好的:

defback(request):
ifrequest.method=='POST':
print(request)
print(request.body)
#print(request.raw_post_data)
xml=request.body
a=ObjectDict(dict((child.tag,child.text)forchildinET.fromstring(xml)))
print(a)
print(a.ToUserName)
#print(a.Event)
returnHttpResponse(json.dumps(a))
returnHttpResponse('hi')
原文链接:https://www.f2er.com/xml/296104.html

猜你在找的XML相关文章