Java XML Child Get getChildElements(Element element)

Here you can find the source of getChildElements(Element element)

Description

Get the Element children of an element as an array.

License

Open Source License

Parameter

Parameter Description
element a parameter

Return

non-null array

Declaration

public static Element[] getChildElements(Element element) 

Method Source Code


//package com.java2s;
/*/*from  www  .jav a 2s  .c  om*/
  XMLUtils.java
    
  (c) 2010-2011 Edward Swartz
    
  All rights reserved. This program and the accompanying materials
  are made available under the terms of the Eclipse Public License v1.0
  which accompanies this distribution, and is available at
  http://www.eclipse.org/legal/epl-v10.html
 */

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Get the Element children of an element as an array.
     * @param element
     * @return non-<code>null</code> array 
     */
    public static Element[] getChildElements(Element element) {
        NodeList childNodes = element.getChildNodes();
        List<Element> kids = new ArrayList<Element>();
        Node node = childNodes.item(0);
        while (node != null) {
            if (node instanceof Element) {
                kids.add((Element) node);
            }
            node = node.getNextSibling();
        }
        return (Element[]) kids.toArray(new Element[kids.size()]);
    }
}

Related

  1. getChildElements(Element element)
  2. getChildElements(Element element)
  3. getChildElements(Element element)
  4. getChildElements(Element element)
  5. getChildElements(Element element)
  6. getChildElements(Element element)
  7. getChildElements(Element element)
  8. getChildElements(Element element)
  9. getChildElements(Element parent)