You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Gavin Bong <ga...@consortio.com> on 2001/01/08 01:49:18 UTC

[ Help: setting node value in Xerces v1.1.2 ]

Hi,

Here's the code I am trying to get to work:

1      DocumentImpl dom = new DocumentImpl(null);
2      Element elt = dom.createElementNS( 
3                            new String("urn:soapfaqsamples:header"),
4                            new String("t:Transaction")
5                    );
6
7      elt.setNodeValue( new String("25") );
8      System.out.println( "Value = " + elt.getNodeValue() ); 

I am getting a null value in line 8. What am I doing wrong ?
Thanks to all.

Regards,

Gavin Bong

...........................................................
Be liberal in what you accept, 
and conservative in what you send
- Jon Postel, RFC1122



Re: [ Help: setting node value in Xerces v1.1.2 ]

Posted by Andy Clark <an...@apache.org>.
Gavin Bong wrote:
> 1      DocumentImpl dom = new DocumentImpl(null);
> 2      Element elt = dom.createElementNS(
> 3                            new String("urn:soapfaqsamples:header"),
> 4                            new String("t:Transaction")
> 5                    );
> 6
> 7      elt.setNodeValue( new String("25") );
> 8      System.out.println( "Value = " + elt.getNodeValue() );
> 
> I am getting a null value in line 8. What am I doing wrong ?

Elements are defined by the DOM specification to always have
a node value of null. Element text is instead stored as Text
node children of the element node. So change to the following:

> 6      Text text = dom.createTextNode("25");
> 7      elt.appendChild(text);
> 8      System.out.println( "Value = " + elt.getFirstChild().getNodeValue() );

Please note that an element can have multiple children of
type element, text, PI, comment, and entity reference. You
should always walk the children of an element to get all of
the text values.

-- 
Andy Clark * IBM, TRL - Japan * andyc@apache.org

RE: [ Help: setting node value in Xerces v1.1.2 ]

Posted by Jay Cain <ja...@cett.msstate.edu>.
Gavin,

Element don't have node values, they have an associated set of attributes
and a list of children. If you want the content of your element to be "25",
then you must create a text node and append it to that element. You can
achieve this by replacing line 7 with the following:

  elt.appendChild(dom.createTextNode("25"));

You also need to change your code that reads out the value in line 8. Here
is simple way of accomplishing this:

  System.out.println("Value = " + elt.getFirstChild().getNodeValue());

Of course, you'll have to write your code to cope with issues such as the
element having no children or multiple children of different types.

Another note: the "new String(...)" statements in your original code are
superfluous. Literal strings in Java are treated as String objects. You can
even access members of a literal just like it were an object (e.g. "literal
text".length()).

- - - - -
Jay Cain
Center for Educational and Training Technology
Mississippi State University


-----Original Message-----
From: Gavin Bong [mailto:gavinb@consortio.com]
Sent: Sunday, January 07, 2001 6:49 PM
To: XercesJ
Subject: [ Help: setting node value in Xerces v1.1.2 ]


Hi,

Here's the code I am trying to get to work:

1      DocumentImpl dom = new DocumentImpl(null);
2      Element elt =
.createElementNS( 
3                            new String("urn:soapfaqsamples:header"),
4                            new String("t:Transaction")
5                    );
6
7      elt.setNodeValue( new String("25") );
8      System.out.println( "Value = " + elt.getNodeValue() ); 

I am getting a null value in line 8. What am I doing wrong ?
Thanks to all.

Regards,

Gavin Bong

...........................................................
Be liberal in what you accept, 
and conservative in what you send
- Jon Postel, RFC1122



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org