Ability to avoid <![CDATA[ and ]]> tags to be converted when unparsing
See original GitHub issueWhen programatically creating for example Android XML strings files using your library’s unparse functionality the library converts “<![CDATA[" into "& lt;![CDATA[" and "]]>” into “]]& gt;” (Spaces added between for the two html entities so things show up)
I don’t know about other xml readers, but the Android SDK’s way of allowing developers to add string resources that contain HTML symbols etc is to use CDATA tags like that. I.e. you wrap your html string in CDATA tags and the android sdk doesn’t go crazy when trying to parse the xml.
This is the xml I want to construct:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="some_html"><![CDATA[<a href="http://www.space.com">Hi my name is Bob</a>]]></string>
</resources>
This is what I pass to unparse:
val = '<![CDATA[<a href="http://www.space.com">Hi my name is Bob</a>]]>'
strings = [{"@name": "some_html", "#text": val}]
xmld = {"resources": {"string": strings}}
xml = xmltodict.unparse(xmld, pretty=True)
And this is the result:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="some_html"><![CDATA[<a href="http://www.space.com">Hi my name is Bob</a>]]></string>
</resources>
I can’t find a way to tell unparse not to try anything fancy for this particular value, but no luck, and I can’t see anything obvious in your samples. Is there a way to achieve what I’m trying to do here?
Issue Analytics
- State:
- Created 9 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top GitHub Comments
It’s just not possible to do this with xmltodict, it’s not what the library is intended for.
@skela You should leave out
<![CDATA[…]]>
from yourval
.xmltodict
will take care of the escaping/unescaping for you, so you shouldn’t attempt to introduce XML escaping characters into the values. The right thing to do is just haveval = '<a href="http://www.space.com">Hi my name is Bob</a>'
.