52

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Kortex786
  • 1,089
  • 1
  • 8
  • 10
  • 3
    Since 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 Answers5

45

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 !

hairlessbear
  • 341
  • 3
  • 11
Kortex786
  • 1,089
  • 1
  • 8
  • 10
  • 3
    After 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
    This doesn't work with python3.6 so the answer is getting outdated – user541905 May 08 '17 at 16:01
  • 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
8

PyXB: http://pyxb.sourceforge.net/

user9876
  • 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
  • 1
    I'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
  • 1
    Once 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
  • 6
    All 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
    pyxb is at [the end of life](https://github.com/pabigot/pyxb/issues/100) – ibid Dec 15 '20 at 12:17
  • 1
    Yes, 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
8

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!

kojiro
  • 74,557
  • 19
  • 143
  • 201
Matt C
  • 1,431
  • 2
  • 14
  • 18
8

xsdata generates dataclasses from an XSD file. According to the documentation it was inspired by JAXB.

Marduk
  • 982
  • 8
  • 12
1

Look at http://pypi.python.org/pypi/rsl.xsd/0.2.3

Also, you might want http://pyxsd.org/ it works very nicely.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 1
    I 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