如何使用python转义xml属性中的特殊字符

时间:2021-03-28 00:26:13

I was wondering how it is possible to escape special characters in an xml.

我想知道如何在xml中转义特殊字符。

The following example fails sadly:

以下示例遗憾地失败:

xml ="""<?xml version="1.0" encoding="UTF-8" ?>
    <Render value="a &lt b"/>
"""
import xml.etree.ElementTree as ET
p = ET.fromstring(xml)

Output:
ParseError: not well-formed (invalid token): line 2, column 28

输出:ParseError:格式不正确(令牌无效):第2行,第28列

What is the easiest way round this problem?

解决这个问题的最简单方法是什么?

1 个解决方案

#1


Wrong escaping!

Try

<Render value="a &lt; b"/>

<渲染值=“a< b” />

And everything works fine!

一切正常!

#1


Wrong escaping!

Try

<Render value="a &lt; b"/>

<渲染值=“a< b” />

And everything works fine!

一切正常!