Encoding newlines in XML?

S

skip

*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():&amp;#10; return 3&amp;#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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top