I just want to know if there is a program that can convert an XSD file to a Python class as JAXB does for Java?
-
3Since Pythons dynamics means that you can set whatever attributes you want on a class, it really doesn't have field definitions in the same way, so I'm not sure it makes as much sense. But of course you could use getters and setters to make type-checks and whatnot. More interestingly though would be something that creates schema definitions for various Python schema frameworks like Zope schemas, Dexterity or SQLAlchemy. That would really be neat if that existed. – Lennart Regebro Jul 02 '09 at 06:56
5 Answers
generateDS : I think this is the good tool I need
Edit : Actually, generateDS does very well the job !! It generates the Python class with all methods (setters and getters, export to XML, import from XML). It works very well !

- 341
- 3
- 11

- 1,089
- 1
- 8
- 10
-
3After testing generateDS and PyXB I decided to use PyXB, because it has better validation support (patterns) and it's possible to invoke the generator from within a python module (setup.py) without using something like system(). – Enno Gröper Feb 08 '13 at 16:46
-
1@EnnoGröper You should probably generate the classes ahead of time. One of my XSDs results in a 29k line Python file (35k with PyXB). Unless you don't have access to the needed XSD until install/runtime, don't generate the classes at install/runtime. – kitti Mar 05 '13 at 16:39
-
Export XML is really very slow in generateDS.... Is there any other alternative ? Which can be fast and efficient. – Nilesh Mar 09 '16 at 04:27
-
4
-
After trying all the mentioned solutions here I used the xsdata solution, which was the most convenient for me, given it can be easily installed via pip and is managed on GitHub. – Jendrik Feb 07 '22 at 15:39

- 10,954
- 6
- 44
- 66
-
PyXB errors out on an XSD I created using dtd2xsd.pl (generateDS has no problem with this XSD). PyXB also seems to generate larger files (29k lines vs 35k for one of my XSDs). As for speed, compile time will be the same for both (probably a full second - huge files), and at runtime generateDS is quite fast so I haven't bothered to test PyXB's output - it can't really be significantly faster. – kitti Mar 05 '13 at 16:43
-
1I've spent the better part of two days trying to use PyXB. The documentation is terrible, it generates syntax errors when generating from this XSD http://www.companieshouse.gov.uk/ef/xbrl/uk/fr/gaap/ae/2009-06-21/uk-gaap-ae-2009-06-21.xsd, due to the fact that identifiers can't contain dashes. – Tom Busby Jul 24 '14 at 13:26
-
1Once I'd fixed those errors, I just want to start accessing the elements when I've read in the XML.... nope, all the examples use trivial XML without namespaces, and there's almost no API documentation for the objects beyond a bunch of incomprehensible graphs – Tom Busby Jul 24 '14 at 13:27
-
6All in all, to anyone else reading this, stear clear of PyXB. I really don't get the hype, it's incredibly frustrating to work with. – Tom Busby Jul 24 '14 at 13:28
-
3
-
1Yes, PyXB is EOL. I'm using this fork: https://github.com/jonfoster/pyxb But I can't really recommend that for "green field" development. If you're already using PyXB it may help you put off the big rewrite to use something else. – user9876 Dec 30 '20 at 17:37
For anyone coming across this question now (in 2021) I suggest checking out xmlschema
I tried the above suggestions (despite the EOL warnings), but wasn't enjoying the experience. Then I discovered xmlschema, and parsed by data in just 3 lines, including the import.
>> import xmlschema
>> data_schema = xmlschema.XMLSchema('my_schema.xsd')
>> data=data_schema.to_dict('my_data.xml')
The imported data is a nested dictionary, with keys and values that match the schema. Beautiful!
xsdata generates dataclasses from an XSD file. According to the documentation it was inspired by JAXB.

- 982
- 8
- 12
Look at http://pypi.python.org/pypi/rsl.xsd/0.2.3
Also, you might want http://pyxsd.org/ it works very nicely.

- 384,516
- 81
- 508
- 779
-
1I see that pyxsd never got beyond a 0.1 release. Any notable limitations? – Marcin Oct 27 '11 at 14:15
-
@Marcin: "Any notable limitations" is impossible to answer. XSD allows one to write things that aren't *easy* to translate to Python. Is that a "limitation"? And if so, isn't it a limitation in Python itself? The only thing you can do is to use PyXSD for your specific XSD's and see if it works for you. If it doesn't work, then it has a notable limitation. A given set of XSD's may be quite complex or quite simple. – S.Lott Oct 31 '11 at 01:57