2

How to order the input tags in Perl code using XML::Simple module for printing the output in XML format in specified order.. i have tried this

    use XML::Simple;
    use Data::Dumper;
    open (FH,"> xml4.txt") || die (); 
    # create array
    @arr = {
        'name'=>['Cisco102'],
        'SSIDConfig'=>[
                {'SSID'=> [{'name'=>'Cisco102'}]}],
        'connectionType'=>['ESS'],
        'connectionMode'=>['auto'],
        'autoSwitch'=>['false'],
        'MSM'=>[{'security' =>[ { 'authEncryption' =>[{'authentication' => 'open',
                        'encryption' => 'WEP',
                          'useOneX' => 'false'
                                        }],
                      'sharedKey' =>[ {
                                     'keyType' => 'networkKey',
                                     'protected' => 'false',
                                     'keyMaterial' => '1234567890'
                                   }]}]}]};
# create object
$xml = new XML::Simple(NoAttr=>1,RootName=>'WLAN Profile');
# convert Perl array ref into XML document
 $data = $xml->XMLout(@arr,xmldecl => '<?xml version="1.0" encoding="US-ASCII"?>');
# access XML data
print FH $data;

but i am not getting the order which i required..i need the order ->name,SSID Config,Connectionmode,connectiontype,autoswitch,MSM .help me

1
  • 2
    You can't, that's not how XML::Simple works. It doesn't keep the order of elements. Commented Sep 28, 2011 at 6:28

2 Answers 2

2

It looks to me that you want 2 things for your XML:

  • no attributes, hence the NoAttr option in the XML::Simple object creation
  • the order of the elements should be as specified

I am not sure why you don't want attributes in your XML, and why the data structure you use to create it has them. You may want to look into that. In any case XML::Simple gives you this feature.

For the second part, XML::Simple doesn't keep the order, and I have found no way to make it do it, so you will need something else.

For a quick and dirty solution, A little bit of XML::Twig in there would do:

# instead of the print FH $data; line

my $twig= XML::Twig->new( )->parse( $data);
$twig->root->set_content( map { $dtwig->root->first_child( $_) } (qw( name SSIDConfig connectionMode connectionType autoSwitch MSM)) );

$twig->print( \*FH);

A couple more comments:

  • you can't use 'WLAN Profile` as the root tag, XML names cannot include spaces
  • it is generally considered polite, when you ask a question about Perl, to show code that uses strict and warnings
  • the proper way to open the output file would be my $out_file= xml4.txt; open ( my $fh,'>', $out_file) or die "cannot create $out_file: $!"; (or use autodie instead of the die), using 3 args open and lexical filehandles is a good habit (this message from the 3-arg open police department ;--)
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you mirod you have helped me a lott..thanks for your comments also..i want to order the elements of sharedkey also (keyType protected keyMaterial) how do i do that ?? and Can i indent the output using XML::Twig??
@user968434 sure, pass pretty_print => 'indented' either to new or to print: $data->print(pretty_print => 'indented', \*FH);Also, I have fixed the code, it should be autoSwitch (uper case S) and not autoswitch as I had written, which screwed up the output
Thanks,the output is indented now..:) but the order of attributes of sharedKey is still not fixed,I need it as keyType,protected,keyMaterial while the order Iam getting them is keyMaterial, keyType, protected.How do I fix it?
and one more thing i need to add <WLANProfile xmlns="microsoft.com/networking/WLAN/profile/v1"> in the second line of XML file and not just <WLANProfile>.Help me with these two things
i used $twig->set_att('WLANProfile'=>'xmlns="microsoft.com/networking/WLAN/profile/v1"'); then its giving an error "Can't locate object method "set_att" via package "XML::Twig" at new.pl line xx".wat to do?? and tell me how to order the attributes of sharedKey, need it as keyType,protected,keyMaterial while the order Iam getting them is keyMaterial, keyType, protected.How to do it?
|
2

Hashes aren't ordered. You could try using a Tie::IxHash (which looks like a hash, but maintains insertion order) instead of a normal hash. If that doesn't work, XML::Simple won't be of use to you.

1 Comment

it doesn't work. I tried, a long time ago to get the 2 modules to play nice with each other, but it didn't quite work, XML::Simple copies hashes in too many places for it to be simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.