Escaping strings for use in XML

0 votes
I'm using Python's xml.dom.minidom to create an XML document.

Can anyone tell me how to make it escape the strings provided by me so they won't be able to mess up the XML?
Apr 15, 2019 in Python by ana1504.k
• 7,890 points

1 answer to this question.

0 votes
You can try the following:

from xml.dom.minidom import Text, Element

t = Text()
e = Element('p') = '<bar><a/><baz spam="eggs"> & blabla &entity;</>'

Then you will get nicely escaped XML string:

>>> e.toxml()
'<p>&lt;bar&gt;&lt;a/&gt;&lt;baz spam=&quot;eggs&quot;&gt; &amp; blabla &amp;entity;&lt;/&gt;</p>'
answered Apr 15, 2019 by SDeb
• 13,250 points

Related Questions In Python

0 votes
1 answer

Need help extracting a schema to make use for an avro file in Python

Hi, nice question. So what I daily use ...READ MORE

answered Jan 10, 2019 in Python by Nymeria
• 3,520 points
0 votes
0 answers

Is there any module in Python that I can use for Wiki markup?

Hi all, simple question. Is there any ...READ MORE

Jan 22, 2019 in Python by Anirudh
• 2,080 points
0 votes
1 answer

How to use for loop in Python?

There are multiple ways of using for ...READ MORE

answered Mar 4, 2019 in Python by Priyaj
• 57,700 points
0 votes
2 answers

How to use in python for loop not equal marks? example: a!=0

Hello @Azizjon, You can go through the example ...READ MORE

answered Oct 12 in Python by Gitika
• 41,980 points
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,470 views
0 votes
0 answers
0 votes
1 answer

comparing strings in Python using "==" or "is"

is is used for identity testing and ...READ MORE

answered Sep 19, 2018 in Python by SDeb
• 13,250 points
0 votes
1 answer

How to use a global variable in function?

The global variable can be used in ...READ MORE

answered Sep 27, 2018 in Python by SDeb
• 13,250 points