W3C home > Mailing lists > Public > www-dom@w3.org > July to September 1998

Re: New spec. Questions

From: Mike Champion <mcc@arbortext.com>
Date: Thu, 23 Jul 1998 23:28:12 -0400
Message-Id: <98Jul23.232434edt.26886@thicket.arbortext.com>
To: "Dom mailing list" <www-dom@w3.org>
At 05:34 PM 7/23/98 -0400, Frank Boumphrey wrote:
>>>appendChild takes a Node argument and not a string.  Could you try again
>
>Sure. Basically what I am asking is  , once you've created an object how do
>you insert it into the tree.
>
>I start with:
>
><xdoc>
><greeting>Hello XML</greeting>
></xdoc>
>
>I want to end up with
>
><xdoc>
><greeting>Hello XML</greeting>
><greeting>Hello DOM</greeting>
></xdoc>
>
>I create an element greeting with
>
>        document.createElement(greeting)
>
>I create a text node with
>
>        document.createTextNode("Hello DOM")
>
>How do I insert them into my document tree? by append child? by
>insertBefore?

You would setup your tree as follows:

Element greet1 = document.createElement("greeting");
Text    hello1 = document.createTextNode("Hello XML")
greet1.appendChild(hello1);

Element greet2 = document.createElement("greeting");
Text    hello2 = document.createTextNode("Hello DOM")
greet2.insertBefore(hello2, null);  // Alternate way to append

document.appendChild(greet1);
document.appendChild(greet2);

There are may ways to access the nodes in the tree, e.g.

greet1 = document.firstChild;
greet2 = document.lastChild;
or
greet2 = greet1.nextSibling;

or 

Nodelist list = document.childNodes;
greet1 = list.item(0);
greet2 = list.item(1);

Is this any clearer?
Received on Thursday, 23 July 1998 23:27:45 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:13:45 GMT