Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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">
like image 859
Rodolphe Avatar asked Sep 11 '25 11:09

Rodolphe


2 Answers

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)
like image 118
Ward Bekker Avatar answered Sep 13 '25 06:09

Ward Bekker


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.

like image 44
I GIVE CRAP ANSWERS Avatar answered Sep 13 '25 05:09

I GIVE CRAP ANSWERS