Re: yet another xml generator



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 &amp; cc</ghi>
  </inner>
</outer>

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

<outer>
  <inner id='1'>
    <abc/>
    <ghi>bb &amp; 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


.



Relevant Pages

  • Re: multiple siblings, using tDOM to generate XML
    ... A simple way to generate XML is the following: ... proc map { ... return [string map $mapTEXT2XML $str] ... set doc ...
    (comp.lang.tcl)
  • Re: REXML memory consumption
    ... I created a test that consisted of feeding 10 xml ... I would think that memory usage ... Is there something wrong with my understanding of Ruby or does REXML ... str << line ...
    (comp.lang.ruby)
  • Re: REXML memory consumption
    ... I would think that memory usage ... Is there something wrong with my understanding of Ruby or does REXML ... str << line ... No need to read the whole file into a large string before it is parsed as XML. ...
    (comp.lang.ruby)
  • Re: How to check if a string is an XML before calling LoadXML(str)
    ... I am new to C# so I might be asking some silly questions. ... if the str is not XML then I would like to do something else instead of being ... >> return but only throw exception if str is not XML document. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Select Into With SELECT FROM for xml auto
    ... propagate since you will be able to insert and assign FOR XML expressions ... >> SET nocount ON DECLARE @str varchar ...
    (microsoft.public.sqlserver.xml)