Mega Code Archive

 
Categories / Java / XML
 

IterableElement getChildElements(final Element e

/*  * Copyright (C) 2007  Vianney le Clément  *  * This program is free software: you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation, either version 3 of the License, or  * (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program.  If not, see <http://www.gnu.org/licenses/>.  */ import java.util.Iterator; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Util{   /**    * Renvoie les éléments enfants (uniquement de type ELEMENT)    *     * @param e    * @return    */   public static Iterable<Element> getChildElements(final Element e) {     return new Iterable<Element>() {       public Iterator<Element> iterator() {         final NodeList list = e.getChildNodes();         int i = 0;         for (; i < list.getLength(); i++) {           if (list.item(i).getNodeType() == Node.ELEMENT_NODE)             break;         }         final int init = i;         return new Iterator<Element>() {           int cur = init;           public void remove() {             throw new UnsupportedOperationException();           }           public Element next() {             Element item = (Element) list.item(cur);             for (cur++; cur < list.getLength(); cur++) {               if (list.item(cur).getNodeType() == Node.ELEMENT_NODE)                 break;             }             return item;           }           public boolean hasNext() {             return cur < list.getLength();           }         };       }     };   } }