我正在尝试格式化以下XML
<block formula="MY_VAR < 3"><set-variable name="OTHER_VAR"></set-variable></block>
进入
<block formula="MY_VAR < 3">
<set-variable name="OTHER_VAR">
</set-variable>
</block>
由于我的公式中的<
,使用XMLFormatter并获得错误。特别是错误是
expatror:未构型(无效令牌)
当我尝试代码
时my_xml = '<block formula="MY_VAR < 3"><set-variable name="OTHER_VAR"></set-variable></block>'
formatter = xmlformatter.Formatter(indent="1", indent_char=" ", encoding_output="UTF-8", preserve=["literal"])
pretty_xml = formatter.format_string(my_xml)
如何将少于我的公式包含在我的公式中并能够格式化我的XML?
您可以使用xml.sax.saxutils.quoteattr构造XML字符串时逃脱属性值。
>>> my_xml = '<block formula=%s><set-variable name="OTHER_VAR"></set-variable></block>' % su.quoteattr('MY_VAR < 3')
>>> my_xml
'<block formula="MY_VAR < 3"><set-variable name="OTHER_VAR"></set-variable></block>'
如果您不控制XML的构造,此hack将在示例中修复XML:
stack = []
out = []
brackets = '<>'
for c in bad_xml:
if c in brackets:
try:
prev = stack[-1]
except IndexError:
stack.append(c)
out.append(c)
else:
if prev == c:
escaped = '>' if c == '>' else '<'
out.append(escaped)
else:
stack.append(c)
out.append(c)
else:
out.append(c)
my_xml = ''.join(out)