Java XML Element Get getElementsByTagName(Element aElement, String aTagName)

Here you can find the source of getElementsByTagName(Element aElement, String aTagName)

Description

get Elements By Tag Name

License

Open Source License

Parameter

Parameter Description
aNode a parameter
aTagName a parameter

Declaration

public static List<Element> getElementsByTagName(Element aElement,
        String aTagName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 www.isandlatech.com (www.isandlatech.com)
 * 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  ava  2 s  .  c o  m*/
 *    ogattaz (isandlaTech) - initial API and implementation
 *******************************************************************************/

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;

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

public class Main {
    private Document pDom = null;

    /**
     * @param aNode
     * @param aTagName
     * @return
     */
    public static List<Element> getElementsByTagName(Element aElement,
            String aTagName) {

        if (aElement == null) {
            return null;
        }
        List<Element> wElmts = new ArrayList<Element>();
        NodeList wList = aElement.getElementsByTagName(aTagName);
        for (int wI = 0; wI < wList.getLength(); wI++) {
            if (wList.item(wI).getNodeType() == Node.ELEMENT_NODE) {
                wElmts.add((Element) wList.item(wI));
            }
        }
        return wElmts;
    }

    /**
     * @param aTagName
     * @return
     */
    public List<Element> getElementsByTagName(String aTagName) {

        return getElementsByTagName(this.getRootElmt(), aTagName);
    }

    /**
     * @return
     */
    public Element getRootElmt() {

        return getDom().getDocumentElement();
    }

    /**
     * @return
     */
    public Document getDom() {

        return pDom;
    }
}

Related

  1. getElements(Element root, String tagName)
  2. getElements(Element topElm)
  3. getElements(final String elementName, final InputStream is, final boolean onlyValues)
  4. getElementsByName(Element elem, String s)
  5. getElementsByTag(Element element, String tag)
  6. getElementsByTagName(Element element, String tag)
  7. getElementsByTagName(Element element, String tagName)
  8. getElementsByTagName(Element node, String tagName)
  9. getElementsByTagName(Element node, String xmlns, String nodeName)