Xml writing in Python and verifying using XSD

Diez B. Roggisch deetsNOSPAM at web.de
Fri May 27 08:49:00 EDT 2005


Prashanth  Ellina wrote:

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

-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list