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">
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With