Hi,

I am facing an issue with my script while creating a XML .

Below is required structure :-

<opt>
<rule disabled="false" logged="false">
  <appliedToList>
    <appliedTo>
      <name>xyz</name>
      <isValid>true</isValid>
      <type>Distri</type>
      <value>123</value>
    </appliedTo>
  </appliedToList>
</rule>
</opt>

However I am getting below o/p

<opt>
  <rule name="appliedToList">
    <appliedTo>
      <name>xyz</name>
      <isValid>true</isValid>
      <type>Distri</type>
      <value>123</value>
    </appliedTo>
  </rule>
</opt>


My current code is :-

#!/usr/bin/perl

  use strict;
  use warnings;
  use FindBin qw();
  use lib "$FindBin::Bin/../lib";
  use IO::Handle;
  use XML::Simple;
use Data::Dumper ;

  my $xmls = XML::Simple->new(ForceArray => 1);

      my $contents = { rule =>{appliedToList=> { appliedTo => [ ] } } };

              push @{ $contents->{rule}->{appliedToList}->{appliedTo} }, {
                  name => ['test'],
                  value => ['123'],
                  name => [ 'xyz' ],
                  type => ['Distri'],
                  isValid => [ 'true' ],
              };

      open my $xml, '>', "output.xml" or die $!;
      $xml->print($xmls->XMLout($contents));
      $xml->close();

Any insights on what I am doing wrong ?

Regards,
Punit

Reply via email to