Xml writing in Python and verifying using XSD

P

Prashanth Ellina

Hi,

I need to write some data to an xml file. I have an XML Schema defined.
I would like to use some mechanism of writing the data to the xml file
and having exceptions thrown back to me when the data is invalid.

I have looked at xml.dom and xml.dom.minidom but could not figure out
what to do to include xml validation using the xsd while writing.

Any ideas?

Thanks in advance,
Prashanth Ellina
 
D

Diez B. Roggisch

Prashanth said:
Hi,

I need to write some data to an xml file. I have an XML Schema defined.
I would like to use some mechanism of writing the data to the xml file
and having exceptions thrown back to me when the data is invalid.

I have looked at xml.dom and xml.dom.minidom but could not figure out
what to do to include xml validation using the xsd while writing.

Any ideas?

Use StringIO to capture the output of your writings, and use a
xsd-validating parser (not sure which one, but google should help) to
reread that. Or a temporary file.

Apart from that I don't see much chances - which has nothing to do with
python but with xml itself: xml isn't suited for streams, because of the
opening/closing tags - and noone can forsee if an opened tag in the stream
is closed in the future. Only after you finished the writing of that
document itself you can do that.

Similar arguments apply to DOMs. A DTD/XSD might require a certain element
to have a childelement. But when creating the element it has no childs - so
the document would be invalid. In the end you can only validate a finished
dom tree. So the only thing that could be tried is to feed that dom tree
instead of a xml string to a validation process - but if that's doable I'm
not sure, and it doesn't buy you too much performance gain. So stick with
the write/validate process described above.
 
P

Prashanth Ellina

Diez said:
Use StringIO to capture the output of your writings, and use a
xsd-validating parser (not sure which one, but google should help) to
reread that. Or a temporary file.

Thank you very much. I'll try that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top