W3C home > Mailing lists > Public > www-dom@w3.org > January to March 2000

Why not Node.insertAfter()?

From: Eric Richardson <maxwell@telesoft.com>
Date: Wed, 08 Mar 2000 09:27:18 -0700
Message-ID: <38C67F65.E04E4856@telesoft.com>
To: DOM <www-dom@w3.org>
Hi,
I'm working with Dom level 1, xmltr2 package from sun. The following is
a simple example
of what I am doing but using the insertBefore() method.

<!ELEMENT person (phone*, computer*)>
<!ELEMENT phone (#PCDATA)>
<!ELEMENT computer (#PCDATA)>

I'm creating a class that allows the client to add phones and computers
to this
hypothetical person document. I want to add new phones and computers
as new Nodes after the last one in the group. If I keep a reference to
the
last one in the group (phones or computers) then it seems that it would
be easiest and most context
free to do the following.

class Person {
    Document document;
    Element lastPhone;
    Element lastComputer;

void addPhone(String number) {
     // assume last phone not null
    Node parent = lastPhone.getParentNode();
    Element element = document.createElement("phone");
     element.appendChild(document.createTextNode(number));
     parent.insertAfter(element, lastPhone);
    lastPhone = element;
}

What I end up doing is remembering what the firstNode of computer is and
do
an insertBefore. This seems kind of wierd that I need to know the next
unrelated
element and do an insertBefore.

Am I missing something?
Does DOM 2 change this?

Thanks,
Eric :-)
Received on Wednesday, 8 March 2000 11:29:23 GMT

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