How to parse XML

Asked 2 years ago, Updated 2 years ago, 76 views

Read the database storing xml in Python

You have to. What should I do?

<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>

xml python

2022-09-22 22:32

1 Answers

Write the ElementTree module. It is faster and easier to use, such as lxml or celementtree of the same API.

import xml.etree.ElementTree
e = xml.etree.ElementTree.parse('thefile.xml').create getroot() #elementinstance

for i in e.findall('bar'):
    for j in i.findall('type'):
        print(j.get('foobar'))


2022-09-22 22:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.