http://www.perlmonks.org?node_id=62814


in reply to XML::DOM::Parser

Hey, XML::DOM!, My least favorite module!

Here is a more perlish (and which seems to be working fine) version of your code:

#!/bin/perl -w use strict; use XML::DOM; my $parser = new XML::DOM::Parser; my $doc = $parser->parse( \*DATA ) or die "Unable to parse document"; my $root = $doc->getDocumentElement(); # safer than just getting the f +irst # child, in case the document h +as a # DTD or start with comments scanner($root); sub scanner { my ($rt) = @_; my $i=0; foreach my $nde ( $rt->getChildNodes()) # yes it is an +array! { if ( ($nde->getNodeType() == TEXT_NODE ) && ($nde->getData()=~ /\S/s) ) # to avoid extr +a white spaces { #$log->info( $i.$nde->getNodeValue()); print $i++," TEXT /", $nde->getData(), "/\n"; } if ($nde->getNodeType == ELEMENT_NODE) { #$log->info( $i.$nde->getNodeName()); print $i++, " ELEMENT ", $nde->getNodeName(), "\n"; } scanner( $nde ); } } __DATA__ <methodCall>Level1 Text <Level2a>Text at Level2a</Level2a> <Level2b>Text at Level2b</Level2b> </methodCall>

Some explanations:

Replies are listed 'Best First'.
Re: Re: XML::DOM::Parser
by spoddie (Initiate) on Mar 08, 2001 at 05:54 UTC
    Well, thank you very much for this detailed reply. It makes it a lot clearer to me where I am going wrong. I'm still struggling with perl syntax to be honest (and yes I am a C/Java programmer, it probably shows!).

    I hope to try this out when I return to the office tomorrow, hope one day I can be of assistance to someone !