Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

maintaing the order of XML

by thickice97 (Initiate)
on Jan 24, 2008 at 17:13 UTC ( #664085=perlquestion: print w/ replies, xml ) Need Help??
thickice97 has asked for the wisdom of the Perl Monks concerning the following question:

I wrote the following code for parsing an xml file. But with this the order is lost as I am using XML simple. What changes can I make for maintaining the order?
use XML::Simple; my $xml = XMLin('test.xsd'); my $TopLevel = 'DOC'; PrintResults('', $TopLevel); sub PrintResults { my $pre = shift; my $start = shift; return unless $start; $pre .= "." if $pre; #print "$pre$start\n"; my $xml_start = GetNode($start); return unless ref($xml_start); foreach my $key (keys %$xml_start) { if ($key ne "") { print "$pre$key\t$xml_start->{$key}->{minOccurs}\t$xml_start-> +{$key}->{maxOccurs}\n"; } # print "$pre$start.$key\n"; PrintResults("$pre$key", $xml_start->{$key}->{type}); } } sub GetNode { my $name=shift; #print "Name: $name\n"; return {$xml->{'xs:complexType'}->{$name}->{'xs:sequence'}->{' +xs:element'}->{name} => {type=>$xml->{ 'xs:complexType'}->{$name}->{'xs:sequence'}->{'xs:element'}->{type}}} +if exists($xml->{'xs:complexType'}->{$ name}->{'xs:sequence'}->{'xs:element'}->{name}); return $xml->{'xs:complexType'}->{$name}->{'xs:sequence'}->{'x +s:choice'}->{'xs:element'} if exists($ xml->{'xs:complexType'}->{$name}->{'xs:sequence'}->{'xs:choice'}->{'xs +:element'}); return $xml->{'xs:complexType'}->{$name}->{'xs:sequence'}->{'x +s:element'} if exists($xml->{'xs:compl exType'}->{$name}->{'xs:sequence'}->{'xs:element'}); return if exists($xml->{'xs:simpleType'}->{$name}->{'xs:restri +ction'}); die "Node name not found '$name'\n"; }

Comment on maintaing the order of XML
Download Code
Replies are listed 'Best First'.
Re: maintaing the order of XML
by Fletch (Chancellor) on Jan 24, 2008 at 17:19 UTC

    XML::Simple returns the parsed data as a hash and hashes are by definition unordered. If order's important you'll need to step up to a bit more heavyweight solution such as XML::Twig which should preserve the parsed tree in its original order (although accessing an traversal will become a bit more involved, but you should be able to abstract that behind subs fairly easy).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Does that mean I would need to change the whole code? Or could I use the subroutines for the XML::Twig?
        Does that mean I would need to change the whole code?

        Yes.  It wouldn't make much sense having an XML::Simple, if its disadvantages (in some contexts) wouldn't be outweighed by its somewhat simpler usage.

Re: maintaing the order of XML
by chanio (Priest) on Jan 24, 2008 at 23:25 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://664085]
Approved by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2015-11-30 04:05 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (757 votes), past polls