Search code examples
xmlencodingerlang

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

  • I never was able to find a good library for this, so I've created my own decode function.

    decode("&gt;" ++ Rest) ->
     ">" ++ decode(Rest);
    decode("&lt;" ++ Rest) ->
     "<" ++ decode(Rest);
    decode("&quot;" ++ Rest) ->
     "\"" ++ decode(Rest);
    decode([]) ->
     [].
    

    According to wikipedia, there are only five character references for XML, so you should be ok with supporting these five:

    &amp; → & (ampersand, U+0026)
    &lt; → < (less-than sign, U+003C)
    &gt; → > (greater-than sign, U+003E)
    &quot; → " (quotation mark, U+0022)
    &apos; → ' (apostrophe, U+0027)