Java XML Child Get getChildElements(Element parent)

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

Description

The method return list of child elements.

License

Open Source License

Parameter

Parameter Description
parent an org.w3c.dom.Element object.

Return

list of child elements.

Declaration

static public Vector getChildElements(Element parent) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2002-2005 IBM Corporation and others.
 * 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
 *
 * Contributors://from w  w w . j  av  a  2s. com
 *   IBM - Initial API and implementation
 *******************************************************************************/

import java.util.Vector;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    /**
     * The method return list of child elements.
     * 
     * @param parent an org.w3c.dom.Element object.
     * @return list of child elements.
     */
    static public Vector getChildElements(Element parent) {
        if (parent == null)
            throw new IllegalArgumentException("Element can not be NULL");

        Vector vect = new Vector();
        Element elem = getFirstChild(parent);
        while (elem != null) {
            vect.add(elem);
            elem = getNextSibling(elem);
        }
        return vect;
    }

    /**
     * Get the first child element from the input elment.
     * 
     * @param element an Element object.
     * @return the firstchild element.
     */
    public static Element getFirstChild(Element element) {
        // Return the first child element
        return findNextSibling(element.getFirstChild());
    }

    /**
     * Get the next sibling element.
     * 
     * @param element - an Element object.
     * @return the next sibling element.
     */
    public static Element getNextSibling(Element element) {
        // Return next sibling element
        return findNextSibling(element.getNextSibling());
    }

    /**
     * Find the next sibling element.
     * 
     * @param startNode XML start node.
     * @return the next sibling element.
     */
    protected static Element findNextSibling(Node startNode) {
        Node node = null;
        Element returnElement = null;

        // Find the next sibling element
        for (node = startNode; node != null && returnElement == null; node = node.getNextSibling()) {
            // If this node is an element node, then return it
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                returnElement = (Element) node;
            }
        }

        // Return next sibling element
        return (Element) returnElement;
    }
}

Related

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