Click here to get back home

yet another xml generator

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
yet another xml generator Martin Busik 03-17-2008
Posted by Martin Busik on March 17, 2008, 9:38 pm
Please log in for more thread options
Hello,
there are some modules aimed to make the xml generation easy - =

XML::Generator,
XML::Writer and XML:Writer::Simple, for example.

For my needs, these are to "verbose" and there is too much code necessar=
y
to express the structure of the resulting xml. So I wrote a piece of cod=
e
which uses a bit more declarative approach. If somebody finds it useful,=

I'd put it on CPAN - or submit it to an existing module.

Let's look at this code:

$str =3D element "outer", content {
        element "inner" =3D> content {
                attribute id =3D> 1;
                element "abc", undef;
                opt_element "def", $value;
                element "ghi", "bb & cc";
        }
}

provided that $value contains "dummy", $str contains the following resul=
t:

<outer>
<inner id=3D'1'>
<abc/>
<def>dummy</def>
<ghi>bb &amp; cc</ghi>
</inner>
</outer>

provided that $value is undef or "", $str would contain:

<outer>
<inner id=3D'1'>
<abc/>
<ghi>bb &amp; cc</ghi>
</inner>
</outer>

Another example:

$str =3D opt_element "abc", content {
        opt_element "def", $value
}

provided $value is undef or "", as result, $str would contain "".
If $value would be "zzz", $str would contain:

<abc>
<def>zzz</dev>
</abc>

Any comments?

Cheers,
Martin

Posted by Darin McBride on March 18, 2008, 3:54 pm
Please log in for more thread options
Martin Busik wrote:

> Hello,
> there are some modules aimed to make the xml generation easy -
> XML::Generator,
> XML::Writer and XML:Writer::Simple, for example.
>
> For my needs, these are to "verbose" and there is too much code necessary
> to express the structure of the resulting xml. So I wrote a piece of code
> which uses a bit more declarative approach. If somebody finds it useful,
> I'd put it on CPAN - or submit it to an existing module.

This looks really interesting. What would hold me back, personally, is
integrating it into other methods. For example, I usually have a stock, or
template, XML file, and then use XML::Twig to read that, add/remove/modify
what I need to, and write it out (to a file, to stdout for apache to send,
whatever). If I could do something like:

$twig->insert_decl('after',
twig_element "out", content {
#...
});

that'd be really cool - a huge time-saver on some of my projects.

> Let's look at this code:
>
> $str = element "outer", content {
> element "inner" => content {
> attribute id => 1;
> element "abc", undef;
> opt_element "def", $value;
> element "ghi", "bb & cc";
> }
> }
>
> provided that $value contains "dummy", $str contains the following result:
>
> <outer>
> <inner id='1'>
> <abc/>
> <def>dummy</def>
> <ghi>bb &amp; cc</ghi>
> </inner>
> </outer>
>
> provided that $value is undef or "", $str would contain:

I would suggest that $value as undef should do this, if it's "", then it
should insert the empty element. In fact, I'd merge your element and
opt_element together:

element "abc"; #creates the "abc" element, empty
element "abc", ""; # same thing
element "abc", undef; # no-op

Also, how do I put an element in the middle of the text of another element?
Does this work?

element "p", "a paragraph with some ", element("b", "bold"), " text";

I'm guessing not. If my guess is correct, I'd suggest at least adding that
as a limitation in the POD.

Posted by John Bokma on March 19, 2008, 7:16 pm
Please log in for more thread options

> if all of the above variables contain either undef or "", no output is
> generated
> at all. IMO this makes it more readable.

I recommend to make this a setting for each, i.e.

undef -> generate empty element, or no-op, or exception
"" -> generate empty element, or no-op, or exception

--
John

Arachnids near Coyolillo - part 1
http://johnbokma.com/mexit/2006/05/04/arachnids-coyolillo-1.html

Posted by mirod on March 20, 2008, 6:40 am
Please log in for more thread options

> there are some modules aimed to make the xml generation easy - =A0
> XML::Generator, XML::Writer and XML:Writer::Simple, for example.
>
> For my needs, these are to "verbose" and there is too much code necessary
> to express the structure of the resulting xml. So I wrote a piece of code
> which uses a bit more declarative approach. If somebody finds it useful,
> I'd put it on CPAN - or submit it to an existing module.
>
> Let's look at this code:
>
> $str =3D element "outer", content {
> =A0 =A0 =A0 =A0 element "inner" =3D> content {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 attribute id =3D> 1;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 element "abc", undef;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 opt_element "def", $value;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 element "ghi", "bb & cc";
> =A0 =A0 =A0 =A0 }
>
> }
>
> provided that $value contains "dummy", $str contains the following result:=

>
> <outer>
> =A0 <inner id=3D'1'>
> =A0 =A0 <abc/>
> =A0 =A0 <def>dummy</def>
> =A0 =A0 <ghi>bb &amp; cc</ghi>
> =A0 </inner>
> </outer>
>
> provided that $value is undef or "", $str would contain:
>
> <outer>
> =A0 <inner id=3D'1'>
> =A0 =A0 <abc/>
> =A0 =A0 <ghi>bb &amp; cc</ghi>
> =A0 </inner>
> </outer>
>
> Another example:
>
> $str =3D opt_element "abc", content {
> =A0 =A0 =A0 =A0 opt_element "def", $value
>
> }
>
> provided $value is undef or "", as result, $str would contain "".
> If $value would be "zzz", $str would contain:
>
> <abc>
> =A0 <def>zzz</dev>
> </abc>
>

Have you looked at HTML::Element? It lets you create elements from a
Perl structure:

http://search.cpan.org/~petek/HTML-Tree-3.23/lib/HTML/Element.pm#@elements_=
=3D_HTML::Element-%3Enew_from_lol(ARRAYREFS)

Your first example would be:

my $elt =3D HTML::Element->new_from_lol(
[ outer =3D>
[ inner =3D> { id =3D> 1 },
[ 'abc' ],
$value && [ def =3D> $value ],
[ ghi =3D> 'bb & cc' ],
]
]);

print $elt->as_XML;

XML::TreeBuilder is based on HTML::Element and can be used to process
XML.

You could add a function opt_elt to avoid repeating the $value, and
you would be quite close to your original syntax, while getting the
rest of the XML::TreeBuilder fetures for free.

--
mirod



Similar ThreadsPosted
XML::Generator::DBI - use of uninitialized value on line 180 and 187 September 12, 2004, 8:00 pm
Regular Expression Generator June 26, 2006, 1:42 am
Free Random Password Generator January 26, 2005, 10:09 pm
XML::Generator::DBI and NULL values in columns, oh my! March 9, 2006, 2:24 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap