question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to avoid <![CDATA[ and ]]> tags to be converted when unparsing

See original GitHub issue

When 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">&lt;![CDATA[&lt;a href="http://www.space.com"&gt;Hi my name is Bob&lt;/a&gt;]]&gt;</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:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
martinblechcommented, Mar 28, 2016

It’s just not possible to do this with xmltodict, it’s not what the library is intended for.

7reactions
martinblechcommented, Apr 9, 2014

@skela You should leave out <![CDATA[…]]> from your val. 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 have val = '<a href="http://www.space.com">Hi my name is Bob</a>'.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ignore CDATA while xml parsing - Stack Overflow
I want to ignore CDATA tag while parsing because it consider the HTML tag following it as text.Since i want to display the...
Read more >
XML CDATA | How CDATA works in XML with Examples
In simple terms, all the entity references and XML tags are excluded by the processor while parsing and treated as character data. Syntax:....
Read more >
Solved: Parsing XML CData - Power Platform Community
Solved: I have a flow where I make an HTTP request for xml data. I'm using xpath to parse it but it's not...
Read more >
CDATA issue in RFC XML conversion - SAP Community
Hi all, I am trying to consume a webservice from ABAP. One of the webservice method responds with a data holding an xml...
Read more >
View topic - Unable to parse the XML inside the CData Section
When i parse the above cdata section in IIB i am able to get the address ... are being correctly converted if they're...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found