Gabriel
Gabriel

Reputation: 9432

How to escape special characters in xml attribute using python

I was wondering how it is possible to escape special characters in an 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

What is the easiest way round this problem?

Upvotes: 2

Views: 367

Answers (1)

Gabriel
Gabriel

Reputation: 9432

Wrong escaping!

Try

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

And everything works fine!

Upvotes: 1

Related Questions