0

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.

1 Answer 1

1

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.

1
  • Actually I changed My data structure to implement what I am wanting correctly. The first error was trying to store the array when I should of been storing the array reference. I changed My implementation however to have an array of hashes for the arcs and nodes that way I can specify other attributes and when I am passing that through XMLout() I am getting the correct output. Commented Jun 12, 2013 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.