0

我有一个散列形式

my $hash = {
    'Key' => "ID1",
    'Value' => "SomeProcess"
};

我需要将其转换为表单的 XML 片段

<Parameter key="ID1">Some Process a</Parameter> 
<Parameter key="ID2">Some Process b</Parameter> 
<Parameter key="ID3">Some Process c</Parameter>

如何才能做到这一点?

4

1 回答 1

3

首先,您的示例不是一个有效的 XML文档,因此 XML::Simple 需要一些陪审团才能输出它。似乎期望输出文档,而不是那么多片段。但是我能够使用这种结构生成该输出:

my $xml
    = {
        Parameter => [
          { key => 'ID1', content => 'Some Process a' }
        , { key => 'ID2', content => 'Some Process b' }
        , { key => 'ID3', content => 'Some Process c' }
        ]
    };


print XMLout( $xml, RootName => '' ); # <- omit the root

请记住,XML::Simple 将无法重新读取它。

这是输出:

  <Parameter key="ID1">Some Process a</Parameter>
  <Parameter key="ID2">Some Process b</Parameter>
  <Parameter key="ID3">Some Process c</Parameter>

所以如果你能把你的结构变成我给你看的形式,你就可以用RootName => ''参数打印出你的片段。

因此,鉴于您的格式,这样的事情可能会起作用:

$xml = { Parameter => [] };
push( @{ $xml->{Parameter} }
    , { key => $hash->{Key}, content => $hash->{Value} }
    );
于 2011-04-21T14:37:42.777 回答