A simple xml.dom.minidom question

P

Paulo Pinto

Hi All,

I want to have some control of how the xml in writexml() is
written.

Is that possible? Or do I have to do navigate the XML tree by
myself?

Thanks in advance,
Paulo Pinto
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Paulo said:
I want to have some control of how the xml in writexml() is
written.

Depending on precisely what kind of control you want to have:
yes, that is possible.
Is that possible? Or do I have to do navigate the XML tree by
myself?

That may also be the case.

Regards,
Martin
 
P

Paulo Pinto

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>
&quot;value1&quot; &quot;value2&quot;
</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?
 
P

Peter Otten

Paulo said:
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>
&quot;value1&quot; &quot;value2&quot;
</values>

Which I understand, because it is how it should be
in standard XML.

However I am really required to use the first form.

Maybe you can get away with a tiny hack:

import xml.dom.minidom as md
import cStringIO

def wd(writer, data):
data = data.replace("&", "&amp;").replace("<", "&lt;")
writer.write(data)

md._write_data = wd

d = md.parseString("""<values>"v1" "v2"</values>""")
s = cStringIO.StringIO()
d.writexml(s)
print s.getvalue()

Peter
 
P

Paulo Pinto

Thanks, that is what I ended up doing.

However it doesn't sound very clean, anyway it works
now. :)

Thanks everyone for your help
Paulo
 
U

Uche Ogbuji

Paulo Pinto said:
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>
&quot;value1&quot; &quot;value2&quot;
</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
 

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

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top