How to get properly shielded XML in python ethere intact?

I am using python version 2.7.3.

test.txt:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <test>The tag &lt;StackOverflow&gt; is good to bring up at parties.</test>
</root>

Result:

>>> import xml.etree.ElementTree as ET
>>> e = ET.parse('test.txt')
>>> root = e.getroot()
>>> print root.find('test').text
The tag <StackOverflow> is good to bring up at parties.

As you can see, the analyzer should have changed &lt;to <, etc.

What I would like to see:

The tag &lt;StackOverflow&gt; is good to bring up at parties.

Untouched, raw text. Sometimes I like it a lot. Raw.

I would like to use this text as it is for display in HTML, so I do not want the XML parser to not work with it.

Do I need to persuade every line or can there be a different way?

+4
source share
1 answer
import xml.etree.ElementTree as ET
e = ET.parse('test.txt')
root = e.getroot()
print(ET.tostring(root.find('test')))

gives

<test>The tag &lt;StackOverflow&gt; is good to bring up at parties.</test>

Alternatively, you can avoid text with saxutils.escape :

import xml.sax.saxutils as saxutils
print(saxutils.escape(root.find('test').text))

gives

The tag &lt;StackOverflow&gt; is good to bring up at parties.
+2
source

Source: https://habr.com/ru/post/1539563/


All Articles