You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Boris Garbuzov <bo...@borealissoft.com> on 2001/05/04 18:53:40 UTC

general DOM question

I found asymmetry in node.setNodeValue (value) behavior for attributes
and
elements. Namely, I can successfully set value for an attribute, but the
following code
prints null for an element. Why? What then this method do at all for an
element? I
thought, it sets the text value for the element. But instead I have to
create text
element and append it.
--------code-------
Element element = document.createElement ("myName");
element.setNodeValue ("myValue");
System.out.println ("element.getNodeValue() = " +
element.getNodeValue());
---------output---------
element.getNodeValue() = null



Re: general DOM question

Posted by Boris Garbuzov <bo...@borealissoft.com>.
Thanks. Then
1. how do I retrieve text child from the element after appending it?
2.  I heard that several text children can merge under some circumstance.
When?
3. Does Xalan supply any tutorial on its API usage? Especially on its specific
(non-DOM) parts?

--------------------------------------





James Melton wrote:

> This seems to be a common misunderstanding. The Element node does not
> allow you to set a value. What you are thinking of as the Element's
> value is really the value of a Text node, which is the only child of the
> Element node. As you noticed, you must create a separate Text node and
> append it to the Element as a child.
>
> Why is this so? An Element could have many children of differing node
> types, so in general it makes no sense to "set the value of an Element".
> However, an attribute of an Element can only have one value, which is
> easily represented by a string, so there is a reasonable sense in which
> you can "set the attribute of an Element".
>
> Purely for reasons of symmetry, one might argue that setting an
> Element's attribute should be disallowed. Instead you should get an
> Attribute node from an Element and then set that Attribute node's value.
> This seems a bit pedantic though.
>
> Jim.
>
> Boris Garbuzov wrote:
> >
> > I found asymmetry in node.setNodeValue (value) behavior for attributes
> > and
> > elements. Namely, I can successfully set value for an attribute, but the
> > following code
> > prints null for an element. Why? What then this method do at all for an
> > element? I
> > thought, it sets the text value for the element. But instead I have to
> > create text
> > element and append it.
> > --------code-------
> > Element element = document.createElement ("myName");
> > element.setNodeValue ("myValue");
> > System.out.println ("element.getNodeValue() = " +
> > element.getNodeValue());
> > ---------output---------
> > element.getNodeValue() = null
>
> --
>
> ____________________________________________________________
> James Melton                 CyLogix
> 609.750.5190                 609.750.5100
> james.melton@cylogix.com     www.cylogix.com


Re: general DOM question

Posted by James Melton <ja...@cylogix.com>.
This seems to be a common misunderstanding. The Element node does not
allow you to set a value. What you are thinking of as the Element's
value is really the value of a Text node, which is the only child of the
Element node. As you noticed, you must create a separate Text node and
append it to the Element as a child.

Why is this so? An Element could have many children of differing node
types, so in general it makes no sense to "set the value of an Element".
However, an attribute of an Element can only have one value, which is
easily represented by a string, so there is a reasonable sense in which
you can "set the attribute of an Element".

Purely for reasons of symmetry, one might argue that setting an
Element's attribute should be disallowed. Instead you should get an
Attribute node from an Element and then set that Attribute node's value.
This seems a bit pedantic though.

Jim.

Boris Garbuzov wrote:
> 
> I found asymmetry in node.setNodeValue (value) behavior for attributes
> and
> elements. Namely, I can successfully set value for an attribute, but the
> following code
> prints null for an element. Why? What then this method do at all for an
> element? I
> thought, it sets the text value for the element. But instead I have to
> create text
> element and append it.
> --------code-------
> Element element = document.createElement ("myName");
> element.setNodeValue ("myValue");
> System.out.println ("element.getNodeValue() = " +
> element.getNodeValue());
> ---------output---------
> element.getNodeValue() = null

-- 

____________________________________________________________
James Melton                 CyLogix
609.750.5190                 609.750.5100
james.melton@cylogix.com     www.cylogix.com