如何在python etree中正确地转义XML? [英] How do I get properly escaped XML in python etree untouched?

查看:140
本文介绍了如何在python etree中正确地转义XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python 2.7.3版本.

I'm 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>

结果:

>>> 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.

如您所见,解析器必须将&lt;更改为<等.

As you can see, the parser must have changed the &lt;'s to <'s etc.

我想看的东西

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

未修饰的原始文本.有时候我真的很喜欢它.未煮熟.

Untouched, raw text. Sometimes I really like it raw. Uncooked.

我想按原样使用此文本在HTML中显示,因此我不希望XML解析器将其弄乱.

I'd like to use this text as-is for display within HTML, therefore I don't want an XML parser to mess with it.

我是否必须重新转义每个字符串,或者还有其他方法吗?

Do I have to re-escape each string or can there be another way?

推荐答案

import xml.etree.ElementTree as ET
e = ET.parse('test.txt')
root = e.getroot()
print(ET.tostring(root.find('test')))

收益

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

或者,您可以使用收益

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

这篇关于如何在python etree中正确地转义XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆