max
max

Reputation: 1589

Extracting data from XML in Erlang

I have this XML:

<message from="adminhash@localhost/xmpphp2" to="queue@springfruitdev"
type="service" id="service1"><body>JSON</body></message>

I want to extract text between tags.

Here's how it's been represented in erlang:

{xmlel,<<"message">>, [{<<"xml:lang">>,<<>>}, {<<"from">>,<<"adminhash@localhost/xmpphp2">>}, {<<"to">>,<<"queue@springfruitdev">>}, {<<"type">>,<<"service">>}, {<<"id">>,<<"service1">>}], [{xmlel,<<"body">>,[],[{xmlcdata,<<"JSON">>}]}]}}

What i've tried by pattern matching:

element(1,list_to_tuple(element(4,element(3,X)))).

Result:

{xmlel,<<"body">>,[],[{xmlcdata,<<"JSON">>}]}

First of all, can i extract xmlcdata in more elegant way?

Upvotes: 2

Views: 2710

Answers (2)

Nuno Freitas
Nuno Freitas

Reputation: 993

Another option in xml.erl is the function

xml:get_path_s/2

TL;DR;

to get body cdata you would use the following

>xml:get_path_s(El,[{elem,"body"},cdata]).
"JSON"

MORE DATA

You provide the Xml has the first argument, and the second argument is list with the following sintax,

  • {elem,StringWithTagName} - a subtag of El
  • {attr, StringWithAtributeName} - a atribute of El
  • cdata - the cdata of El

I will provide the following examples for better understanding. Imagine that your xml is the following

<message from="adminhash@localhost/xmpphp2" to="queue@springfruitdev"
type="service" id="service1">
   <body>JSON</body>
   <writer id="123">
     <name>William</name>
     <country>England</country>
   </writer>
</message>

You have the following invocations and results.

>xml:get_path_s(El,[{elem,"body"},cdata]).
"JSON"

>xml:get_path_s(El,[{elem,"body"}]).
{xmlel,"body",[],[{xmlcdata,<<"JSON">>}]}

>xml:get_path_s(El,[{attr,"type"}]).
"service"

>xml:get_path_s(El,[{elem,"writer"},{elem,"name"},cdata]).
"William"

>xml:get_path_s(El,[{elem,"writer"},{attr,"id"}]).
"123"

Upvotes: 7

legoscia
legoscia

Reputation: 41618

You can use some of the functions in xml.erl:

> xml:get_tag_cdata(xml:get_subtag(X, <<"body">>)).
<<"JSON">>

That is, get the body child element, and then get all character data from it.

Upvotes: 4

Related Questions