Handling quotes in xml.dom text nodes

C

Chaim Krause

I am building a web page (HTML 4.01 Transitional) using
xml.dom.minidom. I have created a <script> node and I have added the
Javascript as a child text node. The issue is that the Javascript
includes quotes that I want to survive when I write the XML to a file.
The issue for me is that they are translated into &quot;.

I know that this is the expected behavior, but I cannot find a manner
to override this behavior to have the quotes survive.

For example, what I want:
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);

What I get:
var map = new
google.maps.Map(document.getElementById(&quot;map_canvas&quot;),
myOptions);

What do you suggest I do to get the desired behavior without rewriting
xml.dom? Or is overriding the method the best way to go?
 
C

Chaim Krause

I am looking to find the best answer to my question, but in the mean
time I have resorted to monkey patching.

def _write_data_no_quote(writer, data):
"Writes datachars to writer."
data = data.replace("&", "&amp;").replace("<", "&lt;")
data = data.replace(">", "&gt;")
writer.write(data)

minidom._write_data = _write_data_no_quote

Maybe this is the best way to do this. I'm not sure.
 
S

Stefan Behnel

Chaim Krause, 13.04.2010 17:26:
I am building a web page (HTML 4.01 Transitional) using
xml.dom.minidom. I have created a<script> node and I have added the
Javascript as a child text node. The issue is that the Javascript
includes quotes that I want to survive when I write the XML to a file.
The issue for me is that they are translated into&quot;.

You should use an HTML generator tool rather than a generic XML tool like
xml.dom.minidom. You need something that knows that the <script> tag
contains CDATA content in HTML, and that can serialise in an HTML aware way
(self-closing tags, etc.).

Check the Python Wiki or PyPI, they have tons of HTML generators.

Stefan
 
C

Chaim Krause

Stefan,

Thank you. The reason that I am using xml.dom.minidom is that I am
restricted to a stock RHEL5 stack. That means 2.4 with nothing added.
(Welcome to US Army IT !!!)

But, I figured out that I need to back up from xml.dom.minidom to
xml.dom and then I can use createCDATASection and get what I need.

Now I am off to fix the issue solving this unmasked. (Google Maps API
v3 doesn't like to be in an HTML 4.01 Transitional page. Ugh)

Thanks
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top