How do I parse XML in Python?

Answer

accepted

I suggest ElementTree. There are other compatible implementations of the same API, such as lxml, and cElementTree in the Python standard library itself; but, in this context, what they chiefly add is even more speed -- the ease of programming part depends on the API, which ElementTree defines.

After building an Element instance e from the XML, e.g. with the XML function, or by parsing a file with something like

e = xml.etree.ElementTree.parse('thefile.xml').getroot()

or any of the many other ways shown at ElementTree, you just do something like:

for atype in e.findall('type'):print(atype.get('foobar'))

and similar, usually pretty simple, code patterns.

All python Questions

Ask your interview questions on python

Write Your comment or Questions if you want the answers on python from python Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---