[erlang-questions] Removing Element from XML
Wes James
comptekki@REDACTED
Thu May 12 20:12:37 CEST 2011
On Thu, May 12, 2011 at 7:38 AM, Maruthavanan Subbarayan
<maruthavanan_s@REDACTED> wrote:
> Hi,
> I have a xml document like below
> <emp>
> <id>1</id>
> <name>Erlang</name>
> <address>
> <street>Street 1</street>
> <country>Country 1<country>
> </address>
> </emp>
> I have done xmerl_scan:stream, now I need to remove the id , name node such
> that I have only address.
> How do I remove so that I can below.
> <emp>
> <address>
> <street>Street 1</street>
> <country>Country 1<country>
> </address>
> </emp>
> Thanks,
> Marutha
Something like this?? where f.xml is a copy of your sample from your
email pasted in to a file:
-module(px).
-include("/usr/local/lib/erlang/lib/xmerl-1.2.8/include/xmerl.hrl").
-export([t/0]).
-define(F, "f.xml").
t() ->
{P1,_}=xmerl_scan:file(?F),
{_, _, _, _, _, _, _, _, Q9, _, _, _} = P1,
[_, _, _, _, _, R6, _]=Q9,
R6,
S=xmerl:export_simple(
[
{xmlText,[{emp,1}],1,[],"\n",text},
{xmlElement,emp,emp,[],
{xmlNamespace,[],[]},
[],1,[],
[
{xmlText,[{emp,1}],5,[],"\n",text},
R6,
{xmlText,[{emp,1}],7,[],"\n",text}
],
[],".",undeclared}
],
xmerl_xml),
% lists:flatten(S)
io:format("~s~n", [S]).
$ erl
Erlang R14B03 (erts-5.8.4) [source] [smp:4:4] [rq:4] [async-threads:0]
[hipe] [kernel-poll:false]
Eshell V5.8.4 (abort with ^G)
1> c(px).
{ok,px}
2> px:t().
<?xml version="1.0"?>
<emp>
<address>
<street>Street 1</street>
<country>Country 1</country>
</address>
</emp>
ok
3>
lines such as:
{xmlText,[{emp,1}],1,[],"\n",text},
were put in for line feeds for viewing purposes, they are not
necessary for real processing.
references:
http://www.erlang.org/doc/apps/xmerl/xmerl_ug.html
http://www.erlang.org/doc/man/xmerl.html
http://stackoverflow.com/questions/1227241/building-an-xmerl-document-in-erlang
-wes
More information about the erlang-questions
mailing list