[
Lists Home |
Date Index |
Thread Index
]
>First, get rid of the global, like so:
>
>>
>>
>* public List getChildren(Element element) {
>>
>* List elements = new ArrayList();
>> elements.add(element);
>>
>> Node child;
>> Node next = (Node) element.getFirstChild();
>> while ((child = next) != null) {
>> next = child.getNextSibling();
>> if (child.getNodeType() == Node.ELEMENT_NODE) {
>> getChildren((Element) child);
>> }
>> }
>* return elements;
>> }
Unfortunately that won't work - the list is created afresh on every call, so
will only contain the last element encountered.
>Next, get rid of all the carriage returns.
;-)
Paul Brown sent me a way of getting rid of the global that should work, just
lugging the list along for every call :
public ArrayList getElements(Document d) {
return getElements(d.getDocumentElement(),new ArrayList());
}
01> private ArrayList getElements(Element e, ArrayList l) {
03> Node next = e.getFirstChild();
04> do {
05> if (next.getNodeType() == Node.ELEMENT_NODE) {
06> getElements(e,l).add(e);
07> }
08> } while ((next = next.getNextSibling()) != null);
09> return l;
10> }
>That knocks it down to about one line of code. I like my Chianti very dry,
>thank you.
Me too!
Thanks,
Danny.
|