+ 18

Is there a 100% effective method to convert or import a complex XML file into Python?

I tried with several tools that I found on Internet. But, all of them are only suitable for simple XML files. ElementTree, DOM, SAX, Soap were some of them! Using the search bar did not give me a satisfactory result either. A book of Python for Data Science gave me some insights, but I am unsure if it's viable or not. Maybe, there is a possibility with LXML but using a C++ library and with more advanced XPath expressions. If you have a solution or easier way to accomplish it, I would really appreciate your help. Thanks!

2nd Feb 2019, 6:09 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
5 Answers
+ 19
Have you tried beautifulsoup? It is really cool for parsing HTML, but also XML. It is very flexible and logical, thus should have no problem with dealing with files of any given complexity.
2nd Feb 2019, 7:55 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 9
Kuba Siekierzynński I haven't reviewed in detail that one but it also seems to be a good option together with lxml or html5lib parsers. I'm gonna try!
2nd Feb 2019, 8:18 PM
Geovanny Martínez Forero
Geovanny Martínez Forero - avatar
+ 6
I've used lxml for python for age. It has support for both HTML and XML. It supports different parsers for different charsets/encodings. Importing from an url, a file or a string containing the content of the xml should be no problem.
3rd Feb 2019, 12:36 AM
+ 3
BeautifulSoup does a good job at this. Lxml is good too
4th Feb 2019, 3:32 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
- 2
diese Frage gehört nicht hierhin. Stellt doch die Fragen, wo es Sinn macht, statt da alles zu verstopfen, wo ich Antworten zu konkreten Fragen suche
5th Feb 2019, 11:37 AM
Christian Wittwer