How to use "&" and other HTML entities as text content?
See original GitHub issuexmlutil version: v0.80.0
platform: Android
Hi, I was decoding some XML from a third party and I noticed that decodeFromString
was failing when the XML response had “&
” or other HTML entities (like “'
” for single quote) as text content inside an element/tag.
After finding that, I tried to create a simple example and I would like some help to understand how to deal with “&”.
Example:
If I use encodeToString
to encode the following content:
@Serializable
data class Results(
val itemList: List<Item>
)
@Serializable
data class Item(
@XmlValue(true)
val text: String
)
[...]
val content =
Results(
listOf(
Item("Item ' 1"),
Item("Item & 2")
)
)
I get this XML as a result:
<Results>
<Item>Item ' 1</Item>
<Item>Item & 2</Item>
</Results>
and now, if I use that same result output with decodeFromString
I get an error
nl.adaptivity.xmlutil.XmlException: Found unexpected child tag: ENTITY_REF
I also noticed that, if I remove @XmlValue(true)
and use “&” inside an attribute
<Results>
<Item text="Item ' 1" />
<Item text="Item & 2" />
</Results>
the decoding goes perfectly.
What’s the correct way of decoding XML that has “&
” as text inside an element?
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
How to use an entity with textContent - Stack Overflow
As answered by others, you can't use HTML entities with textContent , and there is no need to do so. Just use the...
Read more >Exploring The Interplay Between HTML Entities And ...
Ben Nadel explores the interplay between HTML markup that was generated using HTML entities and the subsequent textContent representation of ...
Read more >HTML Entities - W3Schools
HTML Entities ... Reserved characters in HTML must be replaced with character entities. HTML Entities. Some characters are reserved in HTML. If you...
Read more >Common HTML entities used for typography - W3C Wiki
There are a number of HTML entities that come in handy when there's a need for first-rate typesetting. Many of those listed in...
Read more >Text To HTML Entities Encoder/Decoder - Isotropic
This tool is pretty easy to use. Paste your text into the input field, and click submit. It'll spit out the encoded html,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Now fixed in 0.80.1
@micwallace I’m not sure what you mean about this. Do you mean that it stores the text in memory/Kotlin without entities (unescaped) and only encodes on serialization. That is something that cannot really be changed.