Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] Siblings

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Elliotte Rusty Harold <elharo AT metalab.unc.edu>
  • To: "Michael Fitzgerald" <mike AT wyeast.net>, "XOM" <xom-interest AT lists.ibiblio.org>
  • Subject: [XOM-interest] Siblings
  • Date: Tue, 22 Oct 2002 10:09:44 -0400

At 6:29 AM -0700 10/22/02, Michael Fitzgerald wrote:
Likewise, I think

nu.xom.Node getNextSibling()
nu.xom.Node getPreviousSibling()


You know, these methods have bothered me for a while, and I think I've finally put my finger on the problem, and maybe come up with a solution. The problem is that they don't really fit well into XOM's mostly indexed style of access. I don't find that code tends to use them a lot. Instead it just loops over all the children using an index. The ideas mostly come out of DOM, but XOM does things differently. They also don't really belong in Attribute, where they always return null.

Perhaps instead, both of these methods should be replaced with the following:

public int position()

(Alternate names: indexOf(), index())

This method would return the current position of this node in its parent. I actually already have such a method, but it's package private. At least one person has asked for it already; and it might be useful in itself. Anybody who really needed to ask for the next or previous sibling of this node could just ask for node.getParent().getChild(node.position() + 1) or node.getParent().getChild(node.position() - 1). However, I really don't think this is a common use-case. DOM uses these for navigation. XOM doesn't.

This would also fix the attribute problem. Attributes do have an index already, which is purely for navigational convenience. I think it would make sense to use that here too.

Performance wise this would be no more expensive than what getNextSibling() and getPreviousSibling() do now, and could be more easily optimized if that proves important.

I also note that I use the current package private index() method quite frequently inside XOM. I think there might be call for it in some external packages for XPath, XSLT, XInclude, and so forth. I do need to expose enough functionality that not everything has to go into the core package. This feels to me like it would help a lot.

Open questions (assuming this is a good idea at all):

1. What should position() return if the parent is null? 0? -1? throw an exception?

2. What's the right name for this?

Thoughts?

--

+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo AT metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+
| XML in a Nutshell, 2nd Edition (O'Reilly, 2002) |
| http://www.cafeconleche.org/books/xian2/ |
| http://www.amazon.com/exec/obidos/ISBN%3D0596002920/cafeaulaitA/ |
+----------------------------------+---------------------------------+
| Read Cafe au Lait for Java News: http://www.cafeaulait.org/ |
| Read Cafe con Leche for XML News: http://www.cafeconleche.org/ |
+----------------------------------+---------------------------------+




Archive powered by MHonArc 2.6.24.

Top of Page