Java XML Element Get by Name getElementsByTagName(Element parent, String tagName)

Here you can find the source of getElementsByTagName(Element parent, String tagName)

Description

Returns an Element array of all first level descendant Elements with a given tag name, in the order in which they are encountered in the DOM.

License

Open Source License

Declaration

public static Element[] getElementsByTagName(Element parent, String tagName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 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 a  v  a 2 s .  co  m
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.util.Vector;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    /**
     * Returns an Element array of all first level descendant Elements with a
     * given tag name, in the order in which they are encountered in the DOM.
     * Unlike the JAXP apis, which returns preorder traversal of this Element
     * tree, this method filters out children deeper than first level child
     * nodes.
     */
    public static Element[] getElementsByTagName(Element parent, String tagName) {
        NodeList allChildElements = parent.getElementsByTagName(tagName);
        Vector vector = new Vector();
        for (int i = 0; i < allChildElements.getLength(); i++) {
            // we know that the nodelist is of elements.
            Element aElement = (Element) allChildElements.item(i);
            if (aElement.getParentNode().equals(parent))
                // first level child element. add it.
                vector.add(aElement);
        }
        Element[] filteredElements = new Element[vector.size()];
        vector.copyInto(filteredElements);
        return filteredElements;
    }

    /**
     * Same as getElementsByTagName(Element parent, String tagName) but the
     * parent element is assumed to be the root of the document.
     * 
     * @see getElementsByTagName(Element parent, String tagName)
     */
    public static Element[] getElementsByTagName(Document dom, String tagName) {
        NodeList allChildElements = dom.getElementsByTagName(tagName);
        Vector vector = new Vector();
        for (int i = 0; i < allChildElements.getLength(); i++) {
            // we know that the nodelist is of elements.
            Element aElement = (Element) allChildElements.item(i);
            if (aElement.getParentNode().equals(dom.getDocumentElement()))
                // first level child element. add it. Cant use getParent
                // here.
                vector.add(aElement);
        }
        Element[] filteredElements = new Element[vector.size()];
        vector.copyInto(filteredElements);
        return filteredElements;
    }
}

Related

  1. getElements(Element parentElement, String nodeName)
  2. getElements(final Element parent, final String name)
  3. getElementsByName(Element parent, String name)
  4. getElementsByTagName(Document doc, String tagName)
  5. getElementsByTagName(Element parent, String name, boolean localOnly)
  6. getElementsByTagName(final Element parentElement, final String elementName)
  7. getElementsByTagNameCaseInsensitive( Document doc, final Set lowerCaseNames)
  8. getElementValue(Element parent)
  9. getElementValue(Element parent, String tagName)