Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Handling quotes in xml.dom text nodes

12 views
Skip to first unread message

Chaim Krause

unread,
Apr 13, 2010, 11:26:26 AM4/13/10
to
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?

Chaim Krause

unread,
Apr 13, 2010, 11:52:44 AM4/13/10
to
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.

Stefan Behnel

unread,
Apr 13, 2010, 12:35:00 PM4/13/10
to pytho...@python.org
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

Chaim Krause

unread,
Apr 13, 2010, 12:45:52 PM4/13/10
to
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

0 new messages