Encoding newlines in XML?

skip at pobox.com skip at pobox.com
Tue Mar 21 09:50:27 EST 2006


*argh!*  I hate XML!   There, now that that's off my chest...

I am trying to save Python code as attributes of an XML tag with
xml.dom.minidom machinery.  The code, predicatbly enough, contains newlines.
If I do nothing to my program text, upon output I get XML which looks like
this:

    <SomeTag text="def _f():
    return 3
"/>

When that is later parsed, the newlines are replaced by spaces.  That's
clearly no good.

I verified manually that if I changed the above to

    <SomeTag text="def _f():
    return 3
"/>

when read in, the entities are replaced by newlines and the function is
restored to its normal indented, multiline self.  "No problem," I thought,
just update the call the setAttribute() on the node:

    node.setAttribute(key, str(val).replace("\n", "
"))

Alas, that leaves me with this tag:

    <SomeTag text="def _f():&#10;    return 3&#10;"/>

which doesn't work for obvious reasons.

Is there a way to achieve XML attribute newline nirvana?  Please show me the
path to enlightenment.

Thx,

Skip



More information about the Python-list mailing list