Beginner wrote:

I have been trying to output XML that looks like this:

<users>
        <username id="10">dermot</username>
        <username id="17">joe</username>
...
</users>

I have tried XML::Simple and XML::Generator but keep hitting the same problem when it comes to separating the id attribute and the value from my hash reference.

$VAR1 = {
           'dermot' => '10',
           'joe' => '17',
           'rose' => '11',
           'phil' => '13',
           'brian' => '20',
           'andy' => '15',
         };

If I use this loop to generate the output with XML::Generator I only get the last user in the XML

my $xml;
my $gen = XML::Generator->new(':pretty');
foreach my $k (keys %{$ref}) {
               $xml = $gen->users(
                          $gen->username({ id => $ref->{$k}},$k),
                       );
       }
print $xml;

<username id="15">andy</username>

With XML::Simple I considered using this:
my $xml = XMLout($ref, RootName => 'users', ValueAttr => { '??' => '??'}

But I can't determine how to use ValueAttr to suit my needs as the keys/values are not know.

I think XML::Generator would suit me best but I can't see can append data to $xml. Can anyone offer any advice?

I am unfamiliar with XML::Generator, but have experimented with it for
the purposes of your question and it seems to be essentially a translator
that will convert a Perl fragment into an XML fragment, and I can see no
way to modify existing XML. Because of this it seems extremely limited in
its application, as you would have to write

 my $xml = $gen->users(
   $gen->user({id => 10}, 'dermot'),
   $gen->user({id => 17}, 'joe'),
     :
 );

to create your desired output, which simply moves your problem to one of
how to generate the Perl from your data in the first place. If someone
on the list is familiar with XML::Generator then please correct me if I
am wrong.

Depending on how complex your real data is, you should be using XML::LibXML,
XML::Twig, or XML::Element (in decreasing order of desirability) to get
a proper API for building XML data. But if you really have something as
simple as you describe then you should look at XML::Writer, which does little
except check that your XML syntax is correct as you write it (you have to
balance your own start/end tags). The code below represents a solution using
this module.

HTH,

Rob


use strict;
use warnings;

use XML::Writer;

chomp (my @data = <DATA>);

my $writer = XML::Writer->new(DATA_MODE => 1, DATA_INDENT => 1);

$writer->startTag('users');

foreach (@data) {

 my ($name, $id) = split;
 $writer->startTag('user', id => $id);
 $writer->characters($name);
 $writer->endTag('user', id => $id);
}

$writer->endTag('users');

$writer->end;

__DATA__
dermot 10
joe 17
rose 11
phil 13
brian 20
andy 15


**OUTPUT**

<users>
<user id="10">dermot</user>
<user id="17">joe</user>
<user id="11">rose</user>
<user id="13">phil</user>
<user id="20">brian</user>
<user id="15">andy</user>
</users>


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to