Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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 !

In reply to XML::DOM::Parser by spoddie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2023-02-05 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (31 votes). Check out past polls.

    Notices?