A simple xml.dom.minidom question

Uche Ogbuji uche at ogbuji.net
Mon May 10 12:33:43 EDT 2004


Paulo Pinto <paulo.pinto at cern.ch> wrote in message news:<c7fhhn$548$1 at sunnews.cern.ch>...
> There are a set of applications that use XML files
> as their configuration mechanism. Inside these files
> there is some data that isn't standard XML but it in
> form expected by the tools.
> 
> For example
> 
> <values>
> "value1" "value2"
> </values>
> 
> Now, if I use writexml(), I get the following,
> 
> <values>
> "value1" "value2"
> </values>
> 
> Which I understand, because it is how it should be
> in standard XML.
> 
> However I am really required to use the first form.
> 
> So I guess that the only way with xml.dom.minidom is
> to write my own code to tranverse the XML tree. Right?

FWIW, you can do this if you use the 4XSLT Python API to generate XML
as discussed in:

http://www.xml.com/pub/a/2003/10/15/py-xml.html

For one thing, this API does not escape quotes in content.  And if you
need to preserve other characters from escaping you can do so by
telling the output handler to always output values as a CDATA section,
which would yield

<values><![CDATA[
"value1" "value2"
]]></values>

The Python would be along the lines of

import sys
from Ft.Xml.Xslt.XmlWriter import XmlWriter
from Ft.Xml.Xslt.OutputParameters import OutputParameters

oparams = OutputParameters()
oparams.cdataSectionElements = [u'values']
writer = XmlWriter(oparams, sys.stdout)
writer.startDocument()
writer.startElement(u'values')
writer.text(u'"value1" "value2"')
writer.endElement(u'values')
writer.endDocument()

--Uche
http://uche.ogbuji.net



More information about the Python-list mailing list