首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >内部带有转义html的etree解析xml

内部带有转义html的etree解析xml
EN

Stack Overflow用户
提问于 2021-06-02 16:07:10
回答 1查看 29关注 0票数 1

我有一个xml文件,里面有转义的html,字段看起来像这样:

<title>Some records title with html &lt;i&gt; This should be inside escaped html &lt;/i&gt;, end of the title</title>

我发现这个元素很好:

代码语言:javascript
复制
el = titles.find("x:title", NS)

但当我这么做的时候:

el.text

它返回带有非转义标记的文本:

'Some records title with html <i> This should be inside escaped html </i>;, end of the title'

为甚麽呢?我是否必须再次单独转义html标记,即使它是转义的?我希望能够为xml提供转义和非转义的html标记(有时将其显示为文本,有时显示为格式化文本)。如何正确地提供它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-03 03:04:23

使用ElementTree函数时,可以使用_escape_attrib()

代码语言:javascript
复制
import xml.etree.ElementTree as ET

text = '''<title>Some records title with html &lt;i&gt; This should be inside escaped html &lt;/i&gt;, end of the title</title>
'''

root = ET.fromstring(text)

print(ET._escape_attrib(root.text))

这将输出Some records title with html &lt;i&gt; This should be inside escaped html &lt;/i&gt;, end of the title

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67801253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档