drools-eclipse/org.guvnor.eclipse.webdav/src/interface/org/eclipse/webdav/dom/ElementEditor.java
public static void setChild(Element parent, Element child, String[] names, boolean firstToLast) {
        Assert.isTrue(isDAVElement(parent));
        Assert.isNotNull(child);
        Assert.isTrue(DAV_NS.equals(resolve(getNSPrefix(child), parent)));
        Assert.isNotNull(names);

        boolean found = false;
        String name = getNSLocalName(child);
        for (int i = 0; !found && i < names.length; ++i) {
            found = names[i].equals(name);
        }
        Assert.isTrue(found);

        Node sibling = getChild(parent, name, names, firstToLast);

        if (isDAVElement(sibling, name)) {
            parent.replaceChild(child, sibling);
        } else if (firstToLast) {
            if (sibling == null) {
                parent.appendChild(child);
            } else {
                parent.insertBefore(child, sibling);
            }
        } else {
            Node refChild = null;
            if (sibling == null) {
                refChild = parent.getFirstChild();
            } else {
                refChild = sibling.getNextSibling();
            }
            if (refChild == null) {
                parent.appendChild(child);
            } else {
                parent.insertBefore(child, refChild);
            }
        }
    }
public static void setChild(Element parent, Element child, String[] names, boolean firstToLast) {
        Assert.isTrue(isDAVElement(parent));
        Assert.isNotNull(child);
        Assert.isTrue(DAV_NS.equals(resolve(getNSPrefix(child), parent)));
        Assert.isNotNull(names);

        boolean found = false;
        String name = getNSLocalName(child);
        for (int i = 0; !found && i < names.length; ++i) {
            found = names[i].equals(name);
        }
        Assert.isTrue(found);

        Node sibling = getChild(parent, name, names, firstToLast);

        if (isDAVElement(sibling, name)) {
            parent.replaceChild(child, sibling);
        } else if (firstToLast) {
            if (sibling == null) {
                parent.appendChild(child);
            } else {
                parent.insertBefore(child, sibling);
            }
        } else {
            Node refChild = null;
            if (sibling == null) {
                refChild = parent.getFirstChild();
            } else {
                refChild = sibling.getNextSibling();
            }
            if (refChild == null) {
                parent.appendChild(child);
            } else {
                parent.insertBefore(child, refChild);
            }
        }
    }
<p>Sets the given child element as a child of the given parent.
<p>Children are positioned in the order specified by the given names.
If a child with the same name already exists, it is replaced. If
firstToLast is true, the search for the child's position starts at
the parent's first child, otherwise, the search starts at the
parent's last child.
<p>The parent must not be <code>null</code> and must be a WebDAV
element. The child must not be null and its namespace prefix must
resolve to the WebDAV namespace URL in the parent. The parent's valid
child names must not be <code>null</code>, and must contain the name
of the child.

@param parent      the parent to which the child is added
@param child       the child which is added to the parent
@param names       the ordered collection of valid child names for
the parent
@param firstToLast a boolean specifying the direction to search for
the child's position among the parent's children