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

Hi all, I m using the XML::Simple module to create the xml file from the hashes and arrays. It seems that the xml nodes that are getting created are not in the sequence in which I put them in hash/array structre. I want the xml nodes/childnodes to be created in sequence. If anybody has any idea or suggession please help me out. Thanks, Sham.

Replies are listed 'Best First'.
Re: XML::Simple for sequence in xsd
by moritz (Cardinal) on Aug 18, 2007 at 12:13 UTC
    A hash doesn't store the items sequencially, so you can't rely on any ordering in hashes.

    If you want your XML output to be conformant to a XSD, you might want to use some other XML modules.

    (Perhaps it can be accomplished with XML::Simple, but I don't know how).

      Hi Perl 6 in Geraman, Thanks for ur co-operation. If u are knowing other modules that can be used in place of XML::Simple please suggest me for that. I will be greatful to you. Thanks. Sham.
Re: XML::Simple for sequence in xsd
by syphilis (Archbishop) on Aug 18, 2007 at 12:18 UTC
    To follow up on moritz's reply, if you want to order your hash, you can use Tie::IxHash.

    See perldoc -q "hash remember"

    Cheers,
    Rob
      Thanks Rob. I did use that. I thought it will work fine for me but it is showing me following error. Can't locate object method "TIESCALAR" via package "Tie::IxHash" I tried to figure out the error but I could not. I also installed the other required modules , I think I am missing somthing please help/suggest. Thanks. Sham.
        Here's an example of how to use Tie::IxHash:
        use strict; use warnings; use Tie::IxHash; my %myhash; my %myotherhash; tie %myhash, 'Tie::IxHash'; for (my $i=0; $i<7; $i++) { $myhash{$i} = 2*$i; } my @keys = keys %myhash; print "@keys\n"; for (my $i=0; $i<7; $i++) { $myotherhash{$i} = 2*$i; } @keys = keys %myotherhash; print "@keys\n"; __END__ Output: 0 1 2 3 4 5 6 6 4 1 3 0 2 5
        Note that %myhash preserves the order, whereas %myotherhash (which is not tied to Tie::IxHash) does not preserve the order.

        If you still have trouble with the error you experienced it's probably best to provide some code that demonstrates the problem. (Try to provide a small script that demonstrates the problem.)

        Cheers,
        Rob