tags:

views:

53

answers:

2

I have a array reference which has some number of array references inside it. The nested array references also contains the array references. (This is the tree style of XML::Parser.)

my $Filename = "sample.xml";

my $Parser = new XML::Parser( Style => 'tree' );

my $Tree = $Parser->parsefile( $Filename );

Here the $Tree is the array reference it will be array reference , the contents and the nested depth all depends on the xml file. I want to traverse through the nested array $Tree and print the contents.

+1  A: 

Here's a simplistic version:

use strict;
use warnings;

sub printElement
{
  my ($tag, $content) = @_;

  if (ref $content) {
    # This is a XML element:
    my $attrHash = $content->[0];

    print "<$tag>";           # I'm ignoring attributes

    for (my $i = 1; $i < $#$content; $i += 2) {
      printElement(@$content[$i, $i+1]);
    }

    print "</$tag>";
  } else {
    # This is a text pseudo-element:
    print $content;             # I'm not encoding entities
  }
} # end printElement

sub printTree
{
  # The root tree is always a 2-element array
  # of the root element and its content:
  printElement(@{ shift @_ });
  print "\n";
}

# Example parse tree from XML::Parser:
my $tree =
    ['foo', [{}, 'head', [{id => "a"}, 0, "Hello ",  'em', [{}, 0, "there"]],
             'bar', [ {}, 0, "Howdy",  'ref', [{}]],
             0, "do"
            ]
    ];

printTree($tree);

This doesn't print attributes, although you could access them through $attrHash. It also doesn't encode entities in the text, so the resulting output is probably not going to be well-formed XML. I'm leaving those as exercises for the reader. :-)

cjm
Good job mate . I'll modify it for my need .thanks .
karthi_ms
A: 

Data::Walk might be what you're looking for. You can also do that manually with Universal.

hlynur