Re: cloning nodes vs. creating new() ones

Matthew Hixson <[email protected]> Tue, 16 Sep 2003 01:20:33 -0700
Newsgroups gmane.comp.java.enhydra.xmlc
Message-ID <[email protected]>
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?  My understanding is that 
it is a child node of the <option> tag.  Calling 
teacherClone.setNodeValue("foo") didn't do anything.  What node should 
that be setting to "foo" when I call that?
   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.  How can I expect to add text there 
if there isn't an existing node for me to clone?

   Also I tried to do use the <span> tag, as you mentioned, in my HTML:

<option id="teacherName"><span id="teacher">Mrs. 
Wormwood</span></option>

When I run xmlc as

xmlc -keep -class org.poindextrose.school.xmlc.AddStudentForm 
-sourceout ../WEB-INF/classes/ -nocompile -delete-class "DELETE_ME" 
addStudent.html

I see

addStudent.html:61: Warning: discarding unexpected <span>
addStudent.html:61: Warning: discarding unexpected plain text
addStudent.html:61: Warning: discarding unexpected </span>
addStudent.html:61: Warning: discarding unexpected </option>

and no code gets generated for the 'teacher' span element.
   Thanks,
     -M@

On Monday, September 15, 2003, at 01:11  PM, Jacob Kjome wrote:

>
> Very close.  The standard XMLC way to deal with adding multiple 
> dynamic elements to the dom goes like this...
>
> HTMLElement parentHook = xmlcObj.getElementParentHook();
> HTMLElement templateRow = xmlcObj.getElementTemplateRow();
> templateRow.removeAttribute("id");
> Node clonedRow = null;
> while (businessObject.hasMoreElements()) {
>     xmlcObj.setTextProductName(businessObject.getProductName());
>     clonedRow = templateRow.cloneNode(true);
>     parentHook.appendChild(clonedRow);
> }
> parentHook.removeChild(templateRow);
>
> The above implies that the templateRow (whatever element it is, be it 
> a <td>, <li>, <option>, etc...) contains something like <span 
> id="ProductName">dummy data</span>.  You keep on modifying data the 
> XMLC-generated methods and clone the node.  After all that, you get 
> rid of the original template row, leaving only the nodes you inserted 
> containing real data.  Note that we remove the id attribute of the 
> template row before doing any cloning since we don't want to have a 
> bunch of cloned nodes with the same id.  This would result in invalid 
> HTML/XHTML.
>
> Hope that makes sense.  Note how the methodology above makes heavy use 
> of template hooks using id's.  This makes the code more robust by have 
> an absolute reference to particular elements which can be checked at 
> compile time.  Counting on getFirstChild() and such to get to nodes 
> that you assume to be there can fail if the template designer neglects 
> to structure things as you expect or leaves certain things out that 
> you assumed would be there.
>
> Jake
>
> At 11:03 AM 9/15/2003 -0700, you wrote:
>> In my HTML I have a dropdown list with one teacher option.  I'm using 
>> cloneNode() to create another <option> element and calling 
>> cloneNode() on that teacher's <option> to create the text that goes 
>> <option>right here</option>.  I'm using cloneNode() because I don't 
>> know how to go about creating a new object that implements 
>> HTMLOptionElement.  Is the way I'm doing this how its supposed to be 
>> done, by using the w3c API, or is there a better way to instantiate 
>> new XMLC specific objects and add them to the HTMLSelectElement?
>>   Thanks,
>>     -M@
>>
>>                 HTMLSelectElement teacherDropdown = 
>> form.getElementTeacherDropdown();
>>                 HTMLOptionElement teacher1 = 
>> form.getElementTeacherOption();
>>                 HTMLOptionElement teacher2 = (HTMLOptionElement) 
>> teacher1.cloneNode(false);
>> teacher2.appendChild(teacher1.getFirstChild().cloneNode(false));
>>                 teacher2.getFirstChild().setNodeValue("Mr. Snyde");
>>                 teacher2.setValue("2");
>>
>> _______________________________________________
>> XMLC mailing list
>> [email protected]
>> http://www.enhydra.org/mailman/listinfo.cgi/xmlc
>
> _______________________________________________
> XMLC mailing list
> [email protected]
> http://www.enhydra.org/mailman/listinfo.cgi/xmlc
>