19

I am new with python and need to implement an interface to an accounting tool. I received some XSD Files which describes the interface.

What is the easiest way to generate the XML according to the XSD? Is there any module I can use?

Do I have to create the XML all by myself and I can use the XSD just to verify it? How do I best proceed?

1
  • Take a look into PyXb. Xml schema bindings for python. Commented Feb 22, 2018 at 13:48

2 Answers 2

5

I think, generateDS is the solution to your problem. Starting from chapter 5, the command

python generateDS.py -o people.py -s peoplesubs.py people.xsd

reads the XSD file and creates several classes and subclasses. It generates many data structures and getters and setters for accessing and using data :) If there is any XML file that complies with that XSD, it can be read straight away by using

import people
rootObject = people.parse('people.xml')

within the code. More information is given in chapter 12. The aforementioned classes also provide methods to export data as an XML format. The level of documentation is good and it is highly suggested to use this for any future project.

Sign up to request clarification or add additional context in comments.

2 Comments

I think you are missing the point in the question. The OP is asking how to generate an XML (an instance of a schema) from the XSD file. In your answer that would be the generation of people.xml through people.xsd.
I guess that missing part of the answer would be rootObject.export('people.xml'). generating an xml without having data is not possible. // i like the answer. it pointed me to a very complete and easy-to-use framework
0

There are some projects on github that do that by using xmlschema library, for instance fortesp/xsd2xml or miaozn/xsd2xml (python2)

For instance with the former:

xmlgenerator = XMLGenerator('resources/pain.001.001.09.xsd', True, DataFacet())
print(xmlgenerator.execute()) # Output to console
xmlgenerator.write('filename.xml') # Output to file

Unfortunately none of these are properly packaged though.

1 Comment

What is DataFacet(). I cann't find this in any file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.