Re: yet another xml generator
- From: mirod <xmltwig@xxxxxxxxx>
- Date: Thu, 20 Mar 2008 03:40:48 -0700 (PDT)
On Mar 18, 2:38 am, "Martin Busik" <martin.bu...@xxxxxxxx> wrote:
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.
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 & cc</ghi>
</inner>
</outer>
provided that $value is undef or "", $str would contain:
<outer>
<inner id='1'>
<abc/>
<ghi>bb & cc</ghi>
</inner>
</outer>
Another example:
$str = 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>
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_=_HTML::Element-%3Enew_from_lol(ARRAYREFS)
Your first example would be:
my $elt = HTML::Element->new_from_lol(
[ outer =>
[ inner => { id => 1 },
[ 'abc' ],
$value && [ def => $value ],
[ ghi => '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
.
- References:
- yet another xml generator
- From: Martin Busik
- yet another xml generator
- Prev by Date: Re: Increase file reading efficiency
- Next by Date: Re: creating perl binary using PAR module , execution error under unix
- Previous by thread: Re: yet another xml generator
- Next by thread: RFC: Net::CIDR::Lookup
- Index(es):
Relevant Pages
|
|