ElementIterator, as the name suggests, iterates over the
Element tree. The constructor can be invoked with either
Document or an Element as an argument. If the constructor is
invoked with a Document as an argument then the root of the iteration
is the return value of document.getDefaultRootElement().
The iteration happens in a depth-first manner. In terms of how boundary conditions are handled:
next is called before first or current,
the root will be returned
next returns null to indicate the end of the list
previous returns null when the current element is the
root or next has returned null
The ElementIterator does no locking of the Element tree. This
means that it does not track any changes. It is the responsibility of the
user of this class, to ensure that no changes happen during element
iteration.
Simple usage example:
public void iterate() {
ElementIterator it = new ElementIterator(root);
Element elem;
while (true) {
if ((elem = it.next()) != null) {
// process element
System.out.println("elem: " + elem.getName());
} else {
break;
}
}
}
| Modifier and Type | Class and Description |
|---|---|
| private static class | ElementIterator.
The |
| Modifier and Type | Field and Description |
|---|---|
| private Stack | |
| private Element |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public synchronized Object | Returns: a clonedElementIterator ObjectOverrides java. Clones the |
| public Element | Returns: element on top of the stack ornull if the root element
is nullFetches the current |
| public int | |
| private void | |
| public Element | |
| private Element | |
| public Element | |
| public Element |
| elementStack | back to summary |
|---|---|
| private Stack<ElementIterator. | |
| root | back to summary |
|---|---|
| private Element root | |
| ElementIterator | back to summary |
|---|---|
| public ElementIterator(Document document) Creates a new
| |
| ElementIterator | back to summary |
|---|---|
| public ElementIterator(Element root) Creates a new
| |
| clone | back to summary |
|---|---|
| public synchronized Object clone() Overrides java. Clones the
| |
| current | back to summary |
|---|---|
| public Element current() Fetches the current
| |
| depth | back to summary |
|---|---|
| public int depth() Fetches the current depth of element tree.
| |
| dumpTree | back to summary |
|---|---|
| private void dumpTree() Iterates through the element tree and prints out each element and its attributes. | |
| first | back to summary |
|---|---|
| public Element first() Fetches the first element.
| |
| getDeepestLeaf | back to summary |
|---|---|
| private Element getDeepestLeaf(Element parent) Returns the last child of | |
| next | back to summary |
|---|---|
| public Element next() Fetches the next
| |
| previous | back to summary |
|---|---|
| public Element previous() Fetches the previous
| |
StackItem class stores the element as well as a child index.
If the index is -1, then the element represented on the stack is the
element itself. Otherwise, the index functions as an index into the
vector of children of the element. In this case, the item on the stack
represents the "index"th child of the element.
| Modifier and Type | Field and Description |
|---|---|
| pack-priv int | |
| pack-priv Element |
| Modifier and Type | Method and Description |
|---|---|
| protected Object | |
| private Element | |
| private int | |
| private void |
| childIndex | back to summary |
|---|---|
| pack-priv int childIndex | |
| item | back to summary |
|---|---|
| pack-priv Element item | |
| StackItem | back to summary |
|---|---|
| private StackItem(Element elem) | |
| clone | back to summary |
|---|---|
| protected Object clone() throws CloneNotSupportedException Overrides java. Doc from java. Creates and returns a copy of this object. The precise meaning
of "copy" may depend on the class of the object. The general
intent is that, for any object will be true, and that the expression:x.clone() != x will bex.clone().getClass() == x.getClass() true, but these are not absolute requirements.
While it is typically the case that:
will bex.clone().equals(x) true, this is not an absolute requirement.
By convention, the returned object should be obtained by calling
By convention, the object returned by this method should be independent
of this object (which is being cloned). To achieve this independence,
it may be necessary to modify one or more fields of the object returned
by
| |
| getElement | back to summary |
|---|---|
| private Element getElement() | |
| getIndex | back to summary |
|---|---|
| private int getIndex() | |
| incrementIndex | back to summary |
|---|---|
| private void incrementIndex() | |