在Perl中通过XML :: Simple创建子标记

时间:2014-06-04 06:04:36

标签: xml perl perl-module

我正在使用XML::Simple从哈希创建xml,我想在我的XML中的MainRoot之后添加更多的子标记。这是我的哈希

my $args= {
            'KEY1' => "Value1",
            'KEY2' => "Value2",
            'KEY3' => "Value3",
            'KEY4' => "Value4",
            'KEY5' => "Value5",
            'KEY6' => "Value6",
    };


XML::Simple Code

print XMLout($args,NoAttr => 1,RootName => 'MainRoot');

输出

<MainRoot>
  <KEY1>Value1</KEY1>
  <KEY2>Value2</KEY2>
  <KEY3>Value3</KEY3>
  <KEY4>Value4</KEY4>
  <KEY5>Value5</KEY5>
  <KEY6>Value6</KEY6>
</MainRoot>

所需

<MainRoot>
    <SubTag>
      <KEY1>Value1</KEY1>
      <KEY2>Value2</KEY2>
      <KEY3>Value3</KEY3>
      <KEY4>Value4</KEY4>
      <KEY5>Value5</KEY5>
      <KEY6>Value6</KEY6>
     </SubTag>
</MainRoot>

我们怎样才能通过XML :: Simple来做到这一点。请帮助我

1 个答案:

答案 0 :(得分:1)

看起来你可以添加另一个密钥和哈希:

my $args= {
           'SubTag' => {
            'KEY1' => "Value1",
            'KEY2' => "Value2",
            'KEY3' => "Value3",
            'KEY4' => "Value4",
            'KEY5' => "Value5",
            'KEY6' => "Value6",
          },
    };

输出:

<MainRoot>
  <SubTag>
    <KEY1>Value1</KEY1>
    <KEY2>Value2</KEY2>
    <KEY3>Value3</KEY3>
    <KEY4>Value4</KEY4>
    <KEY5>Value5</KEY5>
    <KEY6>Value6</KEY6>
  </SubTag>
</MainRoot>