spoddie has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys, Problem in a nutshell: I cannot retrieve Text node data. I am very new to Perl but need to use it for a project I am involved with. I need to write an XML parser that uses DOM that plugs into the back of apache.
My environment is: Solaris 2.6 Perl 5.6.0 Apache 1.3.12 Mod-perl 1.24 XML::Parser 2.27 XML::DOM 1.25 An example of the XML I am parsing is: <methodCall>Level1 Text <Level2a>Text at Level2a</Level2a> <Level2b>Text at Level2b</Level2b> </methodCall>
(In practice it will be more complex, but that is enough for now..) An exerept from my apache module code is.... (The xml arrives in $buf having been poked in over a socket) and it is all called from the handler() routine.
my $parser = new XML::DOM::Parser; $doc = $parser->parse( $buf ) or die "Unable to parse document"; $root = $doc->getFirstChild(); scanner($root); sub scanner { my ($rt) = @_; my $i; my $nde; my $ndeLst = $rt->getChildNodes(); my $numndes = $ndeLst->getLength() - 1; for ( $i = 0; $i < $numndes; $i++ ) { $nde = $ndeLst->item( $i ); if ($nde->getNodeType() == TEXT_NODE ) { $log->info( $i.$nde->getNodeValue()); print $i,"TEXT", $nde->getData(); } if ($nde->getNodeType == ELEMENT_NODE) { $log->info( $i.$nde->getNodeName()); print $i, "ELEMENT ", $nde->getNodeName(), "\n"; } scanner( $nde ); } }
Although the output is ugly, I believe I should be able to retrieve the Elements <tags> and the text (data). What I actually get is:
0TEXTLevel1 Text 1ELEMENT Level2a 2TEXT 3ELEMENT Level2b
The text elements seem to be lots of white space. I don't think you have to worry about casting in perl so I don't understand why I cannot retrieve the textual data. I do get the first level though. Incidently calling a $root->toString() prints everything so I know it is getting into the tree ok. I would greatly appreciate any advice from you perl gurus out there as I have spent many hours on this without any results !

Replies are listed 'Best First'.
Re: XML::DOM::Parser
by mirod (Canon) on Mar 08, 2001 at 00:16 UTC

    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:

    • Yes there are plenty of white spaces in your document. The DOM states that they should be reported to the application. In your case I think you can ignore "pure white spaces" If you have a choice you should also wrap the text at level 1 in a tag, it would make it easier to ignore the trailing spaces and it would generally be cleaner. Especially if you are dealing with data-oriented XML it is a good idea to avoid mixed-content (text such as Level1 Text and elements (Level2a and Level2b) mixed within an element (methodCall).
    • If you want to program in Java you should write Java code. Seriously XML::DOM uses Perl native types such as arrays, you should take advantage of it. The DOM is enough of a pain as it is, no need to make it even more painful. As for the oft heard argument than this is different than the Java API, even Java programmers seem to be ditching the DOM in favour of JDOM (motto "Say NO to DOM"), a simpler interface to the strict binding defined by the W3C so...
    • use $root=  $doc->getDocumentElement() instead of getFirstChild(), that will save you some trouble the day your XML document comes with a DTD or leading comment, which is perfectly legal
      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 !

Re: XML::DOM::Parser
by rpc (Monk) on Mar 08, 2001 at 00:17 UTC
    I think you're trying to print the wrong Text elements. There appears to be several Text element children of the root node, these are not what you want to print.

    The below XML is what I tested:

    <?xml version="1.0"?> <root> <something>hello</something> <something>world</something> </root>

    And parsed it with the script:

    #!/usr/bin/perl -w use strict; use XML::DOM; my $p = XML::DOM::Parser->new(); my $doc = $p->parsefile('test.xml'); my $root = $doc->getFirstChild(); foreach my $node($root->getChildNodes) { if($node->getNodeType == ELEMENT_NODE) { for my $child($node->getChildNodes) { if($child->getNodeType == TEXT_NODE) { print $child->getData, "\n"; } } } }
    The actual Text nodes you want are contained within Element children of the root node. I don't think you're traversing deep enough into the DOM tree.