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.

How to use "&" and other HTML entities as text content?

See original GitHub issue

xmlutil 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 &amp; 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 &amp; 2" />
</Results>

the decoding goes perfectly.

What’s the correct way of decoding XML that has “&amp;” as text inside an element?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pdvriezecommented, Oct 17, 2020

Now fixed in 0.80.1

0reactions
pdvriezecommented, May 16, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

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