Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access a parse tree created by XML::Parser?

Tags:

xml

perl

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.

like image 680
karthi_ms Avatar asked Aug 04 '10 06:08

karthi_ms


2 Answers

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. :-)

like image 169
cjm Avatar answered Oct 27 '22 18:10

cjm


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

like image 36
hlynur Avatar answered Oct 27 '22 18:10

hlynur