当前位置: 动力学知识库 > 问答 > 编程问答 >

perl - How to expand a hash of arrays to XML using XML::Simple

问题描述:

I have a Perl class that is storing node and arc information for a tree data structure.

When I try to output this as XML using XML::Simple I do not get the full expansion of the arrays.

I have

$table->{arcs} = @arcs;

$table->{nodes} = @nodes;

And when I try to output this as XML I get the following output

<?xml version='1.0'?>

<Root>

<arcs>0</arcs>

<nodes>0</nodes>

</Root>

But the information is stored into the arrays correctly.

Here is the code I am working with

my $xml = new XML::Simple(NoAttr => 1, RootName => 'Root', ForceArray => 1);

$xml->XMLout(

$table,

KeepRoot => 1,

OutputFile => $xml_directory . $out_file . ".xml",

XMLDecl => "<?xml version='1.0'?>",

NSExpand => 0,

ValueAttr => { \@node_values => 'node' }

);

Any ideas on how to expand out the arrays without having to hard code what you want your tags to be?

I would like to be able to go on the fly from data structure to XML for generation.

网友答案:

The statements

$table->{arcs} = @arcs;
$table->{nodes} = @nodes;

are scalar assignments, with the result that the hash elements are set to the number of elements in the respective arrays.

You should change the assignment to assign references to the arrays instead, like this:

$table->{arcs} = \@arcs;
$table->{nodes} = \@nodes;

However this XML result

<Root>
  <arcs>0</arcs>
  <nodes>0</nodes>
</Root>

shows that the sizes you are getting are zeroes, so the arrays are actually empty and this is only part of the story.

Please show your complete code so that we can see where you are going wrong.

分享给朋友:
您可能感兴趣的文章:
随机阅读: