问题
I have some very complex XSD schemas to work with. By complex I mean that each of these XSD would correspont to about 20 classes / tables in a database, with each table having approximately 40 fields. And I have 18 different XSD like that to program.
What I'm trying to achieve is: Get a XML file defined by the XSD and save all the data in a PostgreSQL database using SQLAlchemy. Basically I need a CRUD application that will persist a XML file in the database following the model of the XSD schema, and also be able to retrieve an object from the database and create a XML file.
I want to avoid having to manually create the python classes, the sqlalchemy table definitions, the CRUD code. This would be a monumental job, subject to a lot of small mistakes, given the complexity of the XSD files.
I can generate python classes from XSD in many ways like GenerateDS, PyXB, etc... I need to save those objects in the database. I'm open to any suggestions, even if the suggestion is conceptually different that what I'm describing.
Thank you very much
回答1:
You can use generateDS to create django models from your XSD. You do this using the gends_run_gen_django script, which is located under the django directory in the generateDS source. Here is some documentation on that functionality. Relevant quote:
Here is an overview of the process:
Step 1. Generate bindings -- Run generateDS.py. Step 2. Extract simpleType definitions from schema -- Run gends_extract_simple_types.py. Step 3. Generate models.py and forms.py -- Run gends_generate_django.py.
The script gends_run_gen_django.py performs these three steps.
I believe django should give you most of the functionality of sqlalchemy. However, if you decide to use sqlalchemy instead then generateDS' django functionality might be a good model on which to base a similar sqlalchemy solution.
回答2:
Not sure if there is a way directly, but you could indirectly go from XSD to a SQL Server DB, and then import the DB from SQLAlchemy
来源:https://stackoverflow.com/questions/17750340/generate-python-class-and-sqlalchemy-code-from-xsd-to-store-xml-on-postgres