OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   RE: [xml-dev] code challenge

[ 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







 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS