DOM newbie problem, please help!

J

jet.jetpac

I've tried to write an application using XML::DOM in perl. I'm not able
to getNoddeValue at text node.

XML FILE:
---
<sample param="some param">
<text_node> Test data. </text_node>
</sample>
---

My perl portion:
---
#!/usr/bin/perl

use strict;
use warnings;
use XML::DOM;

my $parser = XML::DOM::parser->new;
my $document = $parser->parsefile("test.xml");
my $value=$document->getDocumentElement->getFirstChild->getNodeValue;

print "$value\n";
----

The problem is, that $value is not set, getNodeValue returns null,
getNodeName returns '#text'. Can anybody PLEASE tell me, what am I
doing wrong? I'm almost ill of the testing...
Thanks for any help,
Peter
 
B

Brian McCauley

I've tried to write an application using XML::DOM in perl. I'm not able
to getNoddeValue at text node.

You are not correctly interpreting your problem.
XML FILE:
---
<sample param="some param">
<text_node> Test data. </text_node>
</sample>
---

My perl portion:
---
#!/usr/bin/perl

use strict;
use warnings;
use XML::DOM;

my $parser = XML::DOM::parser->new;
my $document = $parser->parsefile("test.xml");
my $value=$document->getDocumentElement->getFirstChild->getNodeValue;

print "$value\n";

'null' is not a Perl concept. In Perl there is undef and there is empty
string. However in your test program getNodeValue does not return
either of these. It returns a string consisting of a newline character.
getNodeName returns '#text'. Can anybody PLEASE tell me, what am I
doing wrong? I'm almost ill of the testing...

You do not understand DOM. The element called <text_node> is not a text
node an element node that just happens to be called "text_node". The
text itself is in a text node that is a decendant of this element.

The DOM tree of your document (ignoring the attribute nodes) looks like

Document
\-Element name="sample" <-- this is the documentElement
|-Text data="\n" <-- this is the node you selected
|-Element name="text_node"
| \-Text data=" Test data. "
\-Text data="\n"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top