'How to decode XML entities?
To encode a string to XML, the xmerl_lib:export_text
function does the job, but which function does the opposite job, i.e. converts <
to >
?
I want to convert a complete string like:
<foo="bar">
To:
<foo="bar">
Solution 1:[1]
I never was able to find a good library for this, so I've created my own decode function.
decode(">" ++ Rest) ->
">" ++ decode(Rest);
decode("<" ++ Rest) ->
"<" ++ decode(Rest);
decode(""" ++ Rest) ->
"\"" ++ decode(Rest);
decode([]) ->
[].
According to wikipedia, there are only five character references for XML, so you should be ok with supporting these five:
& ? & (ampersand, U+0026)
< ? < (less-than sign, U+003C)
> ? > (greater-than sign, U+003E)
" ? " (quotation mark, U+0022)
' ? ' (apostrophe, U+0027)
Solution 2:[2]
The exml
package has support for this:
https://github.com/paulgray/exml/blob/master/src/exml.erl#L54
In general, consider exml over xmerl, but be aware it is a NIF-based parser.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ward Bekker |
Solution 2 | I GIVE CRAP ANSWERS |