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)


发下一个更好的:

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



你可能感兴趣的:(pyhon处理xml格式数据)