drools-eclipse/org.guvnor.eclipse.webdav/src/interface/org/eclipse/webdav/dom/ElementEditor.java
public static Element getChild(Element parent, String name, String[] names, boolean firstToLast) {
        Assert.isNotNull(parent);
        Assert.isNotNull(name);
        Assert.isNotNull(names);

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

        int i;
        Node child = null;

        if (firstToLast) {
            i = 0;
            child = parent.getFirstChild();
        } else {
            i = names.length - 1;
            child = parent.getLastChild();
        }

        while (child != null && !names[i].equals(name)) {
            int mark = i;
            while (!isDAVElement(child, names[i]) && !names[i].equals(name)) {
                if (firstToLast) {
                    ++i;
                } else {
                    --i;
                }
            }

            if (!names[i].equals(name)) {
                if (firstToLast) {
                    child = child.getNextSibling();
                } else {
                    child = child.getPreviousSibling();
                }
            } else if (!isDAVElement(child, names[i])) {
                int pos = i;
                found = false;
                while (!found && (pos >= 0 && pos < names.length)) {
                    found = isDAVElement(child, names[pos]);
                    if (firstToLast) {
                        ++pos;
                    } else {
                        --pos;
                    }
                }
                if (!found) {
                    i = mark;
                    if (firstToLast) {
                        child = child.getNextSibling();
                    } else {
                        child = child.getPreviousSibling();
                    }
                }
            }
        }

        return (Element) child;
    }
public static Element getChild(Element parent, String name, String[] names, boolean firstToLast) {
        Assert.isNotNull(parent);
        Assert.isNotNull(name);
        Assert.isNotNull(names);

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

        int i;
        Node child = null;

        if (firstToLast) {
            i = 0;
            child = parent.getFirstChild();
        } else {
            i = names.length - 1;
            child = parent.getLastChild();
        }

        while (child != null && !names[i].equals(name)) {
            int mark = i;
            while (!isDAVElement(child, names[i]) && !names[i].equals(name)) {
                if (firstToLast) {
                    ++i;
                } else {
                    --i;
                }
            }

            if (!names[i].equals(name)) {
                if (firstToLast) {
                    child = child.getNextSibling();
                } else {
                    child = child.getPreviousSibling();
                }
            } else if (!isDAVElement(child, names[i])) {
                int pos = i;
                found = false;
                while (!found && (pos >= 0 && pos < names.length)) {
                    found = isDAVElement(child, names[pos]);
                    if (firstToLast) {
                        ++pos;
                    } else {
                        --pos;
                    }
                }
                if (!found) {
                    i = mark;
                    if (firstToLast) {
                        child = child.getNextSibling();
                    } else {
                        child = child.getPreviousSibling();
                    }
                }
            }
        }

        return (Element) child;
    }
<p>Returns the child WebDAV element of the given parent that is
nearest in position to a WebDAV element with the given name, or
<code>null</code> if no such child exists.
<p>Children are expected to be in the order specified by the given
names. If firstToLast is true, the search for the child 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 name of the child to search for must not be
<code>null</code>. The parent's valid child names must not be
<code>null</code>, and must contain the name of the child being
searched for.
<p>The returned child is as follows:
<ul>
<li>Searching first to last</li>
<ul>
<li>returns <code>null</code> if an element with the given name
should appear as the last child</li>
<li>returns the first occurring child if an element with the given
name is a child</li>
<li>returns a child if an element with the given name would appear
before it</li>
</ul>
<li>Searching last to first</li>
<ul>
<li>returns <code>null</code> if an element with the given name
would appear as the first child</li>
<li>returns the last occurring child if an element with the given
name is a child</li>
<li>returns a child if an element with the given name would appear
after it</li>
</ul>
</ul>
@param parent      the parent of the child being searched for
@param name        the name of the child being searched for
@param names       the ordered collection of valid child names for
the parent
@param firstToLast a boolean specifying the direction to search for
the specified child among the parent's children
@return            a child of the parent, or <code>null</code>