in reply to Re^3: XML::Simple for sequence in xsd
in thread XML::Simple for sequence in xsd

Hi Rob, Tying up the hash is done fantastically with the Tie::IxHash module. But when I pass the this tied hash to the XML::Simple it no more follow the tie for the hash. I am trying to get the XML nodes in the sequence which I am following to create the hash elements. Following is the sample code and the output which I am trying to get correct result. If I am missing somewhere please suggest.
use strict; use XML::Simple; use Data::Dumper; use Tie::IxHash; my $Request_data = {}; tie my %NewOrder_data , 'Tie::IxHash'; %NewOrder_data = ( 'IndustryType' => ['EC'], 'CurrencyCode' => ['840'], 'CurrencyExponent1' => ['1'], 'CurrencyExponent2' => ['2'], ); my @keys = keys %NewOrder_data; print "\n@keys\n"; my $xmlDS = { Request => [ { NewOrder => [ { %NewOrder_data, } ], } ], }; my $request_xml = eval { my $xs = new XML::Simple(keeproot => 1, SuppressEmpty => 1); $xs->XMLout($xmlDS); }; print STDERR "\n\nxml :- >\n" . Dumper($request_xml); __END__ OUTPUT :-------> IndustryType CurrencyCode CurrencyExponent1 CurrencyExponent2 xml :- > <Request> <NewOrder1> <CurrencyCode>840</CurrencyCode> <CurrencyExponent2>2</CurrencyExponent2> <IndustryType>EC</IndustryType> <CurrencyExponent1>1</CurrencyExponent1> </NewOrder1> </Request>
Thanks, Sham

Replies are listed 'Best First'.
Re^5: XML::Simple for sequence in xsd
by syphilis (Archbishop) on Aug 20, 2007 at 11:11 UTC
    For a start, there's one simple change that needs to be made. Replace:
    { %NewOrder_data, }
    with:
    \%NewOrder_data,
    Then, at least, $xmlDS will preserve the order that you assigned. (You can verify that with print Dumper($xmlDS), "\n";)

    However, the order gets lost in the final step when $request_xml is assigned its value. I couldn't find an easy way to deal with that ... perhaps XML::Simple also needs to 'use Tie::IxHash;' for this approach to work.

    Cheers,
    Rob
      I couldn't find an easy way to deal with that

      But moritz had found the solution to that in an earlier post in this thread (moritz++). It's just a matter of replacing:
      my $xs = new XML::Simple(keeproot => 1, SuppressEmpty => 1);
      with:
      my $xs = new XML::Simple(keeproot => 1, SuppressEmpty => 1, NoSort + => 1);
      Here's the (final) amended version of the script:
      use strict; use XML::Simple; use Data::Dumper; use Tie::IxHash; my $Request_data = {}; tie my %NewOrder_data , 'Tie::IxHash'; tie my %r, 'Tie::IxHash'; my $request_xml = \%r; %NewOrder_data = ( 'IndustryType' => ['EC'], 'CurrencyCode' => ['840'], 'CurrencyExponent1' => ['1'], 'CurrencyExponent2' => ['2'], ); my @keys = keys %NewOrder_data; print "\n@keys\n"; my $xmlDS = { Request => [ { NewOrder => [ \%NewOrder_data, ], } ], }; $request_xml = eval { my $xs = new XML::Simple(keeproot => 1, SuppressEmpty => 1, NoSort + => 1); $xs->XMLout($xmlDS); }; print STDERR "\n\nxml :- >\n" . Dumper($request_xml); __END__ Outputs: IndustryType CurrencyCode CurrencyExponent1 CurrencyExponent2 xml :- > $VAR1 = '<Request> <NewOrder> <IndustryType>EC</IndustryType> <CurrencyCode>840</CurrencyCode> <CurrencyExponent1>1</CurrencyExponent1> <CurrencyExponent2>2</CurrencyExponent2> </NewOrder> </Request> ';
      Cheers,
      Rob
        Hi Rob, it working greatly!!!!!!! Now I can move to build the gateway request easily. Thanks and Cheers.... Sham.