|
FYI, perl's object system is not so hot, so if you
want to do any real OO programming (writing classes that use inheritance, etc.)
in perl, you should buy Damien Conway's book on object oriented perl. It
provides work arounds for many of the shortcomings in perl's object
system.
As for traversing the dom tree in perl, I've
included my code for how I did it (this code was initially based on some sample
code provided in the Xerces-Perl distribution). My program defines a
generic tree traversing subroutine that takes a DOM tree and a reference to a
subroutine as arguments. The tree traversing subroutine then applies the
referenced subroutine to each node of the tree (kind of like the way perl's
`map' construct is used to apply a block of code to every element in a
list). Anyway, the thing you'll be interested in is the DOM_traverse( )
function.
Good luck,
-ted
---------------------------------------------
#!/usr/bin/perl -w
use strict; use XML::Xerces;
use Getopt::Long; ## INIT STUFF ## our %OPTIONS;
my $USAGE = <<EOU; USAGE: $0 [-v=xxx][-n] file Options: -v=xxx Validation scheme [always | never | auto*] -n Enable namespace processing. Defaults to off. -s Enable schema processing. Defaults to off. * = Default if not provided explicitly
EOU
my $rc = GetOptions(\%OPTIONS, 'v=s', 'n',
's');
die $USAGE unless $rc; die $USAGE unless scalar @ARGV; my $file = $ARGV[0]; -f $file or die "File '$file' does not exist!\n"; my $namespace = $OPTIONS{n} || 0; my $schema = $OPTIONS{s} || 0; my $validate = $OPTIONS{v} || 'auto'; if (uc($validate) eq 'ALWAYS') {
$validate = $XML::Xerces::DOMParser::Val_Always; } elsif (uc($validate) eq 'NEVER') { $validate = $XML::Xerces::DOMParser::Val_Never; } elsif (uc($validate) eq 'AUTO') { $validate = $XML::Xerces::DOMParser::Val_Auto; } else { die("Unknown value for -v: $validate\n$USAGE"); } ## END INIT ## my $parser = XML::Xerces::DOMParser->new(); $parser->setValidationScheme ($validate);
$parser->setDoNamespaces ($namespace); $parser->setCreateEntityReferenceNodes(1); $parser->setDoSchema ($schema); my $error_handler =
XML::Xerces::PerlErrorHandler->new();
$parser->setErrorHandler($error_handler); eval { $parser->parse(XML::Xerces::LocalFileInputSource->new($file))
};
myDie($@) if $@; my $doc = $parser->getDocument();
my $rootEl = $doc->getDocumentElement(); DOM_traverse($rootEl, \&printPretty);
exit(0);
sub DOM_traverse { my ($node, $subr, $dpth) = @_; $dpth ||= 0;
$subr->($node, $dpth) if ($subr);
my $kid = $node->getFirstChild;
while ( not $kid->isNull )
{ DOM_traverse($kid, $subr, $dpth + 1); $kid = $kid->getNextSibling(); } } sub printPretty { my ($node, $dpth) = @_; # return if empty text node
return if ($node->getNodeName eq "#text" && $node->getNodeValue =~ /^\s*$/); my $indent = (" " x $dpth);
# print node name and value
print ($indent.$node->getNodeName."=".$node->getNodeValue."\n"); # return unless an element-node (cuz only elements have
attributes)
return unless $node->isa('XML::Xerces::DOM_Element'); my %attrs = $node->getAttributes->to_hash();
return unless (%attrs); # return if no attributes
print "$indent <";
while (my ($name, $value) = each %attrs) { print " $name=\"$value\""; } print " >\n"; }; sub myDie { my $err = shift; ref($err) and die $err->getMessage();
die $err;
} __END__
|
Title: Need help traversing DOM tree with xerces in Perl
- Need help traversing DOM tree with xerces in Perl Pam Gage
- Re: Need help traversing DOM tree with xerces in Per... ted sandler
- Re: Need help traversing DOM tree with xerces in... Jason E. Stewart
- Re: Need help traversing DOM tree with xerces in Per... Jason E. Stewart
- RE: Need help traversing DOM tree with xerces in Per... Pam Gage
