Re: cloning nodes vs. creating new() ones

Richard Kunze <[email protected]> Tue, 16 Sep 2003 10:43:50 +0200
Newsgroups gmane.comp.java.enhydra.xmlc
Organization Tivano Software GmbH
Message-ID <[email protected]>
On Tuesday 16 September 2003 10:20, Matthew Hixson wrote:
> Thanks, Jake.  I tried to restructure my code to be more similar to
> what you had, but still ended up with doing it not much different than
> I had before.  This time I populate the list of teachers from a static
> list:
>
> 		HTMLSelectElement teacherDropdown = form.getElementTeacherDropdown();
> 		HTMLOptionElement teacherName = form.getElementTeacherName();
> 		teacherName.removeAttribute("id");
> 		String[] teachers = new String[] { "Mrs. Wormwood", "Mr. Snyde", "Ms.
> Ratchet" };
> 		for(int i = 0;i < teachers.length;i++){
> 			HTMLOptionElement teacherClone = (HTMLOptionElement)
> teacherName.cloneNode(true);
> 			teacherClone.setValue(new Integer(i).toString());
> 			teacherClone.setLabel(teachers[i]);
> 			teacherClone.getFirstChild().setNodeValue(teachers[i]);
> 			teacherDropdown.add(teacherClone, null);
> 		}
> 		teacherDropdown.removeChild(teacherName);
>
> I still can't seem to get away from using getFirstChild() to get ahold
> of the text inside the <option> tag, like so:
>
> <option id="teacherName">Mrs. Wormwood</option>
>
> How do you access the "Mrs. Wormwood" part?  

Use the generated method "setTextTeacherName(String)". This sets the content 
of the first descendant of the node with id "teacherName". In your example, 
thats the first child (i.e. the same node you are using), but the difference 
is that "setText....()" doesn't break the template if e.g. the designer 
decides to make the text bold.

>    Is there a way to make up a new node to add to the <option> tag?  It
> seems that if I have
>
> <option id="teacherName"></option>
>
> that getFirstChild() returns null.  

That's right. There isn't any child node in this case. Simply add a dummy text 
to the template.

Hope this helps,

Richard
-- 
Richard Kunze 

[ t]ivano Software, Bahnhofstr. 18, 63263 Neu-Isenburg
Tel.: +49 6102 80 99 07 - 0, Fax.: +49 6102 80 99 07 - 1
http://www.tivano.de
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQA/Zs1G86Aqb3OZfF4RAjfBAKCzGstz0Q2VR6i0iSi/4aolgEvbzwCgpEF+
7lwkCvZTatrkbx+FoErEubQ=
=38bC
-----END PGP SIGNATURE-----