-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
How to use "&" and other HTML entities as text content? #44
Comments
This is a bug. I'll add a test to fix this. It should "just work". |
… The Android parser is relatively stupid and exposes these. This fixes bug #44
Fixed it in dev. I'll release something tomorrow. |
Now fixed in 0.80.1 |
I am seeing this issue with 0.80.1. Here is a snippet of the XML I am trying to use: <StringWithMarkup>
<String>Chloroacetic acid, >=99%</String>
</StringWithMarkup> (It throws at the The corresponding class is: @XmlSerialName("StringWithMarkup", "http://pubchem.ncbi.nlm.nih.gov/pug_view", "")
@Serializable
data class StringWithMarkup(
@XmlElement(true) @SerialName("String") val string: String = "",
val markup: List<Markup> = emptyList()
) And the format in use: private val xmlFormat = XML {
repairNamespaces = true
policy = DefaultXmlSerializationPolicy(pedantic = false, autoPolymorphic = false) { input, inputKind, name, candidates ->
if (name?.namespaceURI == "http://pubchem.ncbi.nlm.nih.gov/pug_view") {
throw UnknownXmlFieldException(input.locationInfo, name.toString(), candidates)
}
return@DefaultXmlSerializationPolicy
}
} |
Just a note for anyone who still has the same issue. I was able to solve it by adding a normalize function and calling it on the string before calling xml.decodeFromString:
|
Actually this is still not working for other xml entities on Android, such as < This behavior is very strange. These entities shouldn't be decoded until the text is required. < can't be replaced beforehand because it would make the XML invalid. |
@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. |
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:I get this XML as a result:
and now, if I use that same result output with
decodeFromString
I get an errorI also noticed that, if I remove
@XmlValue(true)
and use "&" inside an attributethe decoding goes perfectly.
What's the correct way of decoding XML that has "
&
" as text inside an element?The text was updated successfully, but these errors were encountered: