Java XML Element Get getElements(Element elem, String path)

Here you can find the source of getElements(Element elem, String path)

Description

Returns the list of named descendents.

License

Apache License

Parameter

Parameter Description
elem The parent/context element
path '/' separated path

Return

List of elements as per the path.

Declaration

public static List getElements(Element elem, String path) 

Method Source Code

//package com.java2s;
/*//from   ww  w. ja  v a  2  s  . c om
 * $Id$ 
 *
 * Copyright 1999-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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 {
    /**
     * Returns the list of named descendents. Allows the user
     * to specify '/' separated path to the child element. e.g.
     * 'a/b/c'. Note, that if 'a' has multiple child elements
     * named 'b', the method will go to the first 'b' element,
     * and return all child elements named 'c'. Returns an
     * empty List if the path does not return any elements.
     *
     * @param elem The parent/context element
     * @param path '/' separated path
     * @return List of elements as per the path.
     */
    public static List getElements(Element elem, String path) {
        List list = new ArrayList();
        if (elem == null)
            return list;

        int ndx = path.indexOf("/");
        if (ndx != -1) {
            Element child = getElement(elem, path.substring(0, ndx));
            return getElements(child, path.substring(ndx + 1));
        }
        NodeList nl = elem.getChildNodes();
        for (int i = 0, len = nl.getLength(); i < len; i++) {
            Node node = nl.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE
                    && node.getNodeName().equals(path)) {
                list.add(node);
            }
        }
        return list;
    }

    /**
     * Returns the named descendent. Allows the user
     * to specify '/' separated path to the child element. e.g.
     * 'a/b/c'. Note, that in case of multiple children with the
     * same name, the first child is returned. Returns a null, if
     * the path does not map to any element.
     *
     * @param elem The parent/context element
     * @param path '/' separated path
     * @return Named element as per the path.
     */
    public static Element getElement(Element elem, String path) {
        List elems = getElements(elem, path);
        if (elems.size() == 0)
            return null;
        else
            return (Element) elems.get(0);
    }
}

Related

  1. getElementPath(Element element)
  2. getElementPath(Element element)
  3. getElementPosition(Element element)
  4. getElements(Element aElement)
  5. getElements(Element document, String elementName, String attrName, String attrValue)
  6. getElements(Element element)
  7. getElements(Element element, String tagName)
  8. getElements(Element node)
  9. getElements(Element root)