I am trying to generate an XML file from a given XML schema. I have been able to do it with pyxb library in python. But the problem is as the XSD gets huge it is impossible to manually encode each and evey tag. Is there any python library which creates a data structure from a given XSD file which can be iterated through
-
Can you provide XML schema? – Alderven Feb 14 '19 at 12:14
-
I can't provide you the original xml schema that i am working on but the xml schema at https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/sample-xsd-file-customers-and-orders1 is more or less of the same structure the only difference being the size. Mine is just bigger – bhanu goyal Feb 14 '19 at 13:41
-
So you want to generate XML file with random data based on the XSD? – Alderven Feb 14 '19 at 14:06
-
I want to generate a xml in which some of the fieds will be populated by my UI and rest of the fields can be filled with dummy data. Even if there is a way to generate a XML with random data based on the XSD it will help in solving my purpose upto a large extent. – bhanu goyal Feb 14 '19 at 15:12
3 Answers
This library seems to do what you want: https://pypi.org/project/xmlschema/
After skimming the documentation I have found this code example: https://xmlschema.readthedocs.io/en/latest/usage.html#xsd-declarations
>>> import xmlschema
>>> from pprint import pprint
>>> schema = xmlschema.XMLSchema('xmlschema/tests/test_cases/examples/vehicles/vehicles.xsd')
>>> schema.types
NamespaceView({'vehicleType': XsdComplexType(name='vehicleType')})
>>> pprint(dict(schema.elements))
{'bikes': XsdElement(name='vh:bikes', occurs=[1, 1]),
'cars': XsdElement(name='vh:cars', occurs=[1, 1]),
'vehicles': XsdElement(name='vh:vehicles', occurs=[1, 1])}
>>> schema.attributes
NamespaceView({'step': XsdAttribute(name='vh:step')})
So it looks like it can be used to create a python data-structure you can iterate through from an XSD file.
Also this question might be relevant: How to convert XSD to Python Class

- 349
- 2
- 7
-
I am still not able to figure out what to do with this. I have got a dictionary which contains all the tag names and their restrictions. How to convert this into a xml file is still baffling me – bhanu goyal Feb 14 '19 at 11:08
-
The xmlschema library supports conversion to python dictionaries, and it's very pleasant. – wgwz Nov 07 '21 at 20:26
xsdata generates dataclasses from an XML schema. It can also render a dataclass as XML. A dataclass can be instantiated either by providing all the named parameters to the constructor (which can be statically checked using mypy or pylance) or using a dictionary and dacite.
As explained in the FAQ it is better to use Python 3.10 so that required fields become required arguments of the constructor. In order to do that execute xsdata init-config
and set kwOnly=true
in .xsdata.xml
.

- 982
- 8
- 12
You can generate XML file from XSD file:
import requests
with open('file.xsd', 'r') as f:
data = f.read()
r = requests.post('https://www.liquid-technologies.com/api/Converter', json={"Filename": "schema.xsd", "Type": "xsd", "Data": data, "TargetType": "xml", "Arguments": {"elementName": "Root", "elementNamespace": "", "addAnyAttributes": False, "addAnyElements": False, "forceOptionItemsToDepthOf": "4", "forceXsiNamespaceDelaration": False, "maxDepthToCreateOptionalItems": "7", "indent": "2", "indentChar": " ", "indentAttributes": False, "seed": "9722"}})
with open('file.xml', 'w') as f:
f.write(r.json()['Files'][0]['Data'])

- 7,569
- 5
- 26
- 38
-
Still it wouldn't solve my purpose... I need something explicitly in python which can help me generate xml files – bhanu goyal Feb 16 '19 at 20:34
-
Can you plz explain me what's happening here and how did you do it? – bhanu goyal Feb 21 '19 at 08:38
-
On input you have `file.xsd` with XSD schema. On output you get 'file.xml' generated from that XML. – Alderven Feb 21 '19 at 09:26
-
How were you able to get this API and is there any documentation fori it which I can go through to make it work according to my needs? – bhanu goyal Feb 21 '19 at 09:30
-
There is no documentation but you can check their [website](https://www.liquid-technologies.com/online-xsd-to-xml-converter) for more information – Alderven Feb 21 '19 at 09:34
-