2

I have spent hours looking for this but I couldn't find any answer... I have an XSD file (given by another source) and I am trying to create an XML file that complies with that. I have all the data ready to data structures. All I need to do is to export data as XML BY FOLLOWING the XSD. Is that possible?

I am not looking to export XSD to XML, neither to validate XML nor to parse XML. I start from scratch, I read XSD and based on that I am trying to create XML by mapping my data structures to the allowed elements from XSD.

Conceptually, it seems doable.... however, I haven't found any answer yet. Any ideas and suggestions (preferably in Python 2) are more than welcome.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Christos
  • 31
  • 1
  • 5
  • Way too broad. Sure it is possible, a number of tools can do this. Are you looking for a programmatic approach? Do you just need a single instance? Do you want to programm a tool which does this? Etc. Etc. – lexicore Oct 18 '14 at 18:33
  • I am looking for any approach in python 2 where I will "link" the items from the XSD file with my data structure and at the end the final XML file will be generated. Of course I will need to loop over some cells of my arrays. I am just trying to avoid to write the method that will open and close XML elements and would add attributes. That's the "hard" way and certainly not the most efficient. Let's say that I have the element in the XSD , and I have NOISE=[82, 85, 86]. Somehow I should be able to pass that list to the element and have 82, etc. Is it better now? – Christos Oct 19 '14 at 20:48
  • So you basically want something like JAXB for Java (http://www.vogella.com/tutorials/JAXB/article.html) or my Jsonix for JavaScript (https://github.com/highsource/jsonix). Check this question http://stackoverflow.com/questions/1072853/how-to-convert-xsd-to-python-class and http://sourceforge.net/projects/pyxb/ – lexicore Oct 19 '14 at 21:26
  • many thanks, I did not know how to express it. I'll give it a go and will let you know in due course. Fairwell :) – Christos Oct 20 '14 at 09:17

1 Answers1

1

I sorted it out and I reply to myself for other's benefit.

As nicely proposed, generateDS is the solution to the 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')

whithin 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.

Have fun, C.

Christos
  • 31
  • 1
  • 5