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:
An example of the XML I am parsing is:
<Level2a>Text at Level2a</Level2a>
<Level2b>Text at Level2b</Level2b>
(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();
my ($rt) = @_;
my $ndeLst = $rt->getChildNodes();
my $numndes = $ndeLst->getLength() - 1;
for ( $i = 0; $i < $numndes; $i++ )
$nde = $ndeLst->item( $i );
if ($nde->getNodeType() == TEXT_NODE )
print $i,"TEXT", $nde->getData();
if ($nde->getNodeType == ELEMENT_NODE)
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:
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 !
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||