XML :: Hash :: LX - 将哈希转换为xml:不是预期的输出

时间:2011-01-01 13:17:33

标签: xml perl hash

我正在尝试使用此模块XML::Hash::LX将哈希转换为XML字符串,但我只获得第一个哈希键。有人能告诉我,如何获得整个哈希?

#!/usr/bin/env perl
use warnings;
use 5.012;
use XML::Hash::LX;

my $hash = { one => 1, two => 2, three => 3, four => 4 };

my $str = hash2xml( $hash );
print $str;

# Output:
# <?xml version="1.0" encoding="utf-8"?>
# <three>3</three>

1 个答案:

答案 0 :(得分:5)

你缺少一个根元素。

hash2xml { numbers => { one => 1, two => 2, three => 3, four => 4 } } 

<?xml version="1.0" encoding="utf-8"?>
<numbers><three>3</three><one>1</one><two>2</two><four>4</four></numbers>