List Info

Thread: Question about getNodeName




Question about getNodeName
user name
2006-06-13 18:26:38
When using DOM and getNodeName I get the name and
"#text" returned. Why
is #text returned and how can I have only the name returned?


my $root = $doc->getDocumentElement();
my installinfo = $root->getChildNodes();
foreach my $run (installinfo)
{
        print $run->getNodeName(), "\n";
}

_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
Question about getNodeName
user name
2006-06-13 20:35:24
* Mesdaq, Ali wrote:
>When using DOM and getNodeName I get the name and
"#text" returned. Why
>is #text returned and how can I have only the name
returned? 
>
>my $root = $doc->getDocumentElement();
>my installinfo = $root->getChildNodes();
>foreach my $run (installinfo)
>{
>        print $run->getNodeName(), "\n";
>}

You iterate over the child nodes. A node can be an element,
some text, a
processing instruction, an entity reference, a comment, and
so on. So in
your example you hit a text node and the name of a text node
is "#text".
If you want to skip text nodes, you can e.g. filter the node
type (which
is probably availabe through getNodeType()).
-- 
Björn Höhrmann · mailto:bjoernhoehrmann.de · http://bjoern.hoehrmann.de

Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de

68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/ 
_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
Question about getNodeName
user name
2006-06-13 21:16:37
Mesdaq, Ali wrote:
> When using DOM and getNodeName I get the name and
> "#text" returned. Why is #text returned and
how
> can I have only the name returned?

Because text nodes are nodes. getChildNodes() will return
text nodes,
which don't have names.

> my $root = $doc->getDocumentElement();
> my installinfo = $root->getChildNodes();
> foreach my $run (installinfo)
> {
>         print $run->getNodeName(),
"\n";
> }

Assuming you're using an Xpath-aware parser, you can do:

print map { $_->nodeName . "\n" }
$doc->findnodes('/*/*');

Because * doesn't match text nodes in Xpath.


_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
Question about getNodeName
user name
2006-06-13 20:47:34
On Tuesday 13 June 2006 20:26, Mesdaq, Ali wrote:

Hi,

> When using DOM

I assume you mean XML:OM?

> and getNodeName I get the name and "#text"
returned. Why 
> is #text returned 

It's because you *are* indeed accessing a text node, not a
subelement (which 
is what you seem to expect). Unlike elements, text nodes do
not have names, 
that is why this method of XML:OM
returns '#text' instead. Note that 
white-space also produces text nodes.

> and how can I have only the name returned? 

You didn't indicate how your XML instance looks like. But
assuming it looks 
something like

<doc>
  <foo>...</foo>
</doc>
(e.g doc has - at least - 3 child nodes: a blank text node,
element foo, and 
another blank text node with the newline following
</foo>)

you can either use $root->getElementByTagName('foo',0),
or check the node type 
of each child node (e.g. with $kid->isElementNode or
$kid->getNodeType == 
ELEMENT_NODE) skipping non-element nodes where appropriate.

> my $root = $doc->getDocumentElement();
> my installinfo = $root->getChildNodes();
> foreach my $run (installinfo)
> {
>         print $run->getNodeName(),
"\n";
> }

-- Petr
_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )