如何禁用Perl的XML::Simple中的子标记排序?

时间:2022-12-22 14:30:09

I just want to find the way of disable the sort operation in XML::Simple

我只是想找到在XML中禁用排序操作的方法::Simple。

For example:

例如:

#!/usr/bin/perl

use strict;
use warnings;

use XML::Simple;

my %my_xml = (
    NAME  => [ 'test' ],
    EMAIL => [ 'test@example.com' ],
    ID    => 12,
);

my $xs = XML::Simple->new;
print $xs->XMLout(\%my_xml, RootName => "datas", NoSort => 1);

__END__

I get following output:

我得到以下输出:

<datas ID="12">
  <EMAIL>test@example.com</EMAIL>
  <NAME>test</NAME>
</datas>

But I want the output to be:

但我希望输出是:

<datas ID="12">
  <NAME>test</NAME>
  <EMAIL>test@example.com</EMAIL>
</datas>

How can I achieve this?

我如何做到这一点?

3 个解决方案

#1


5  

It seems that Tie::IxHash can help you.

似乎Tie: IxHash可以帮助您。

In my tests, reversing the email and name lines in the hash in the code below result in them being reversed in the output. I am not sure that would still be the case with more complex data structures, depending on whether XML::Simple reuses the original hash or copies it.

在我的测试中,将下面代码中的散列中的电子邮件和名称行颠倒,会在输出中导致它们被颠倒。我不确定对于更复杂的数据结构仍然是这样,这取决于XML::Simple是重用原始散列还是复制它。

#!/usr/bin/perl

use strict;
use warnings;

use Tie::IxHash;
use XML::Simple;

my( $id, $name, $email)= ( 'i1', 'John Doe', 'jdoe@example.com');

my %my_xml;
tie %my_xml, 'Tie::IxHash';
%my_xml = (
            'EMAIL' => [$email],                   
            'NAME' => [$name],
             'ID'  => $id,
          );

my $xs = XML::Simple->new();
my $xml_ref = \%my_xml ;
my $xml = $xs->XMLout($xml_ref, RootName=> "datas" ,NoSort => 1);

print $xml;

#2


4  

According the Grant McLean (author of XML::Simple)

Grant McLean (XML::Simple的作者)

if we want is for the order of elements from the original document to be retained. Unfortunately, that is not possible with XML::Simple. When the document is parsed, XML::Simple stores the element data in hashes. Hashes do not remember the order in which keys were added so this data is lost.

如果我们想要保留原始文档中元素的顺序。不幸的是,这在XML中是不可能的::Simple。当对文档进行解析时,XML::Simple将元素数据存储在散列中。散列不记得添加键的顺序,因此会丢失数据。

If we want to retain the document order you need to use a different module. he recommends XML::LibXML. In fact he wrote an article about switching from XML::Simple to XML::LibXML here:

如果我们想保留文档顺序,您需要使用不同的模块。他建议使用XML::LibXML。事实上,他写了一篇关于从XML::Simple到XML::LibXML的文章:

http://www.perlmonks.org/index.pl?node_id=490846

http://www.perlmonks.org/index.pl?node_id=490846

#3


0  

The order of elements in a hash table are not guaranteed to be in the order you enter them. Have you tried printing out the elements of the hash table to verify that they are in the order you want?

哈希表中的元素顺序不能保证按照输入它们的顺序。您是否尝试过打印散列表的元素以验证它们是否按照您想要的顺序排列?

Also, the order of elements in an XML file shouldn't matter to any application reading the XML - the information is still there and labeled by the tag.

而且,XML文件中元素的顺序对任何读取XML的应用程序都不重要——信息仍然存在,并由标记进行标记。

Ron

罗恩

This test seems to show that the nosort option is working, it's just the order of the hash:

这个测试似乎显示nosort选项正在工作,它只是哈希的顺序:

use strict;
use XML::Simple;

my $name        = "Ron";
my $email       = "ron.savage\@gmail.com";
my $id          = 5;

my %my_xml = (  'NAME' => $name, 'EMAIL' => $email, 'ID' => $id );

my $var;
my $val;
print "Hash: \n";
foreach $var (keys(%my_xml)) 
   {
   $val = $my_xml{$var};
   print "    ${var}=${val}\n";
   }

my $xs = XML::Simple->new();
my $xml_ref = \%my_xml ;
my $xml = $xs->XMLout($xml_ref, RootName=> "datas" ,nosort => 1);

print "XML:\n".$xml;

output:

输出:

~/dot-dash-dot.com/files >perl testxml.pl
Hash:
    ID=5
    NAME=Ron
    EMAIL=ron.savage@gmail.com
XML:
<datas ID="5" NAME="Ron" EMAIL="ron.savage@gmail.com" />

This is perl, v5.8.4 built for i386-linux-thread-multi

#1


5  

It seems that Tie::IxHash can help you.

似乎Tie: IxHash可以帮助您。

In my tests, reversing the email and name lines in the hash in the code below result in them being reversed in the output. I am not sure that would still be the case with more complex data structures, depending on whether XML::Simple reuses the original hash or copies it.

在我的测试中,将下面代码中的散列中的电子邮件和名称行颠倒,会在输出中导致它们被颠倒。我不确定对于更复杂的数据结构仍然是这样,这取决于XML::Simple是重用原始散列还是复制它。

#!/usr/bin/perl

use strict;
use warnings;

use Tie::IxHash;
use XML::Simple;

my( $id, $name, $email)= ( 'i1', 'John Doe', 'jdoe@example.com');

my %my_xml;
tie %my_xml, 'Tie::IxHash';
%my_xml = (
            'EMAIL' => [$email],                   
            'NAME' => [$name],
             'ID'  => $id,
          );

my $xs = XML::Simple->new();
my $xml_ref = \%my_xml ;
my $xml = $xs->XMLout($xml_ref, RootName=> "datas" ,NoSort => 1);

print $xml;

#2


4  

According the Grant McLean (author of XML::Simple)

Grant McLean (XML::Simple的作者)

if we want is for the order of elements from the original document to be retained. Unfortunately, that is not possible with XML::Simple. When the document is parsed, XML::Simple stores the element data in hashes. Hashes do not remember the order in which keys were added so this data is lost.

如果我们想要保留原始文档中元素的顺序。不幸的是,这在XML中是不可能的::Simple。当对文档进行解析时,XML::Simple将元素数据存储在散列中。散列不记得添加键的顺序,因此会丢失数据。

If we want to retain the document order you need to use a different module. he recommends XML::LibXML. In fact he wrote an article about switching from XML::Simple to XML::LibXML here:

如果我们想保留文档顺序,您需要使用不同的模块。他建议使用XML::LibXML。事实上,他写了一篇关于从XML::Simple到XML::LibXML的文章:

http://www.perlmonks.org/index.pl?node_id=490846

http://www.perlmonks.org/index.pl?node_id=490846

#3


0  

The order of elements in a hash table are not guaranteed to be in the order you enter them. Have you tried printing out the elements of the hash table to verify that they are in the order you want?

哈希表中的元素顺序不能保证按照输入它们的顺序。您是否尝试过打印散列表的元素以验证它们是否按照您想要的顺序排列?

Also, the order of elements in an XML file shouldn't matter to any application reading the XML - the information is still there and labeled by the tag.

而且,XML文件中元素的顺序对任何读取XML的应用程序都不重要——信息仍然存在,并由标记进行标记。

Ron

罗恩

This test seems to show that the nosort option is working, it's just the order of the hash:

这个测试似乎显示nosort选项正在工作,它只是哈希的顺序:

use strict;
use XML::Simple;

my $name        = "Ron";
my $email       = "ron.savage\@gmail.com";
my $id          = 5;

my %my_xml = (  'NAME' => $name, 'EMAIL' => $email, 'ID' => $id );

my $var;
my $val;
print "Hash: \n";
foreach $var (keys(%my_xml)) 
   {
   $val = $my_xml{$var};
   print "    ${var}=${val}\n";
   }

my $xs = XML::Simple->new();
my $xml_ref = \%my_xml ;
my $xml = $xs->XMLout($xml_ref, RootName=> "datas" ,nosort => 1);

print "XML:\n".$xml;

output:

输出:

~/dot-dash-dot.com/files >perl testxml.pl
Hash:
    ID=5
    NAME=Ron
    EMAIL=ron.savage@gmail.com
XML:
<datas ID="5" NAME="Ron" EMAIL="ron.savage@gmail.com" />

This is perl, v5.8.4 built for i386-linux-thread-multi