jdtoronto has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks,

Seems my brain is rather fritzed on this problem right now. I have this hash (from Data::Dumper and shortened for clarity):

$VAR1 = { 'campaign' => { 'menuid' => '10118', 'action' => 0, 'phonenumbers' => [ { 'phonenumber' => { 'number' => '9163723752' } }, { 'phonenumber' => { 'number' => '9166413900' } } ] } };
Which I need to convert to XML that looks like this:
<campaign action="0" menuid="10118"> <phonenumbers> <phonenumber number="9163723752" /> <phonenumber number="9166413900" /> </phonenumbers> </campaign>
But which comes out like this:
<campaign action="0" menuid="10118"> <phonenumbers name="phonenumber" number="9163723752" /> <phonenumbers name="phonenumber" number="9166413900" /> </campaign>
My code looks like this:
print Dumper $xml_hash; my $xml_obj = XML::Simple->new( Keeproot => 1, ForceArray => 1); my $xmlo = $xml_obj->XMLout( $xml_hash ); print $xmlo;
But it seems to make no difference what I do with the ForceArray option at all!

Any help appreciated! jdtoronto

Replies are listed 'Best First'.
Re: Yet another XML::Simple question!
by ikegami (Patriarch) on Jul 19, 2005 at 19:52 UTC

    Your syntax for calling XMLout is wrong. It's just print XMLout($xml);.

    The reason it's printing wrong is that your structure is wrong. You have:

    'phonenumbers' => [ # <- Repeats phonennumbers { 'phonenumber' => { 'number' => '9163723752' } }, { 'phonenumber' => { 'number' => '9166413900' } } ]

    You need:

    'phonenumbers' => [ # <- Repeats phonennumbers { 'phonenumber' => [ # <- Repeats phonennumber { 'number' => '9163723752' }, { 'number' => '9166413900' } ] } ]

    For example:

    >type !.xml <campaign action="0" menuid="10118"> <phonenumbers> <phonenumber number="9163723752" /> <phonenumber number="9166413900" /> </phonenumbers> </campaign> >type !.pl use XML::Simple qw( XMLin XMLout ); # It didn't think "!.xml" was a file name until I added "./". # Options were lowercase in older versions (like the one I have. my $xml = XMLin('./!.xml', keeproot => 1, forcearray => 1); print XMLout($xml, keeproot => 1); >perl !.pl <campaign menuid="10118" action="0"> <phonenumbers> <phonenumber number="9163723752" /> <phonenumber number="9166413900" /> </phonenumbers> </campaign>

    Update: Sorry, I was familiar with the OO calling syntax. My initial paragraph was wrong, but the rest still applies.

      ikegami

      Well that makes sense! And it works too.

      When I am finished this job I will take the time to sit down and read something on the subject, this has beena harrowing experience and I have to say that the documentation I have found is obtuse to say the least.

      Thank you for your assistance

      jdtoronto

        Yeah, like what's the point of all that morphing?
Re: Yet another XML::Simple question!
by gellyfish (Monsignor) on Jul 19, 2005 at 20:11 UTC

    Although you may be best following ikegami's advice and altering your structure, you can in fact get it to output exactly as you want from your existing structure with:

    my $xml_obj = XML::Simple->new( Keeproot => 1, GroupTags => {phonenumbers => phonenum +ber}, KeyAttr => [-name] );
    Which may or may not be uglier than you expected.

    /J\