[
Lists Home |
Date Index |
Thread Index
]
Nope, that won't work, you can't construct a new loop in every call of
the method.
Try passing the list in as a parameter and using the NodeList, like
this:
public void getChildren( Element element, List elements ) {
elements.add( element );
NodeList list = element.getChildNodes();
for( int i = 0; i < list.getLength(); i++ ) {
if( list.item( i ).getNodeType() == Node.ELEMENT_NODE ) {
getChildren( ( Element )list.item( i ), elements );
}
}
}
Toivo
-----Original Message-----
From: Jeff Lowery [mailto:jlowery@scenicsoft.com]
Sent: Thursday, August 08, 2002 5:30 PM
To: 'Danny Ayers'; Xml-Dev
Subject: RE: [xml-dev] code challenge
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;
> }
-- Jeff
|