I am trying to produce XML from erlang values and return that XML via socket.
So given,
Value = 2, Type = "3", Scope = anatom
I would like
<?xml version="1.0"?>
<result value=2 type="3" scope="anatom" />
What I get in this instance is escaped and has quotes around both the 2 and 3:
"<?xml version=\"1.0\"?><result value=\"2\" type=\"3\" scope=\"anatom\"/>"
If any of these values has a list, as in
Value = 2, Type = "3", Scope = [1,2]
I get something like [60,63,120,109, …] when I would like
<?xml version="1.0\"?> <result value=2 type="3" scope=[1,2]/>"
I have an xml formatting routine that looks like
format_return({ok, {V, T, S}}) ->
Data = {result,
[{value, V}, {type, T}, {scope, S}],
[]},
xmerl_ucs:to_utf8(xmerl:export_simple([Data], xmerl_xml)).
And its called by
...
Reply = xml_formater:format_return(Reply),
{ok, Reply, State}
The Reply, above, is passed back to my socket-generic-behavior which, in turn, results in
gen_tcp:send(Socket, io_lib:fwrite("~p~n",[Reply])),
Could some kind person please put me out of my misery?
there seems to be two issues:
A) this is not well-formed XML —
so you won’t be able to generate it.
B) the reason that you see the output as a list of integers is because of the relationship of lists and strings in Erlang – basically “abc” is equivalent to [$a,$b,$c] (or [97,98,99]). the erlang pretty-printer will display a list with all printable characters as the string equivalent. [1]
your Scope variable [1,2] is equivalent to a string comprised of two non-printable characters (ascii 1, ascii 2). the xml routines don’t care that it’s non-printable. the output is displayed as a list of integers because it contains non-printable characters.
if you change your Scope variable to [97,98] you’ll see that it displays as “ab” in the resulting xml.
so.. if you are ok with the well-formed result —
your Scope variable needs to be the string “[1,2]” (or [ $[, $1, $,, $2, $] ] or [91,49,44,50,93]).
[1] this is a simplification — there are dozens of others posts that explain this in better detail. https://stackoverflow.com/search?q=erlang+lists+and+strings